Anvil Community Forum

Create Data Tables with Code

Is there a way to create/drop data tables from Python code?

10 Likes

I’m afraid not; I am therefore moving this to Feature Requests.

I am, however, now wondering why you want to create tables dynamically…

1 Like

I have several similar apps that rely on the same underlying table structure and it’s a pain to re-create them each time!

Is there a reason they can’t share a table? (You can add a table from another app, instead of creating a new one, from the Add Table button)

Yep - these have the same structure but independent content.

1 Like

+1 to this, working on a modular library that would be an underlying dependency for other apps, but would rely on tables to exist in the dependent app. Need to detect non-existence of a table and programmatically create and in some cases, populate the new table with some seed data.

3 Likes

@shaun How about this one? Is it on the issue tracker?

1 Like

Has any headway been made on this feature?

Can we just have an “Auto-create missing tables when adding rows” check box in the Data Tables service?

Please? Building layered app dependencies infrastructure (core library app supplying universally needed packages, with industry-specific app depending on core app, adding additional packages, and then client-specific app depending on industry app) and when new modules/packages are implemented and then enabled in the client-specific app, need the code to automatically create missing tables and fields in the client-specific app.

Do not want to share these tables between apps, they are client specific.

2 Likes

I believe this ties in with

Copy Data Tables Structure With Optional Data

and

Cloning apps and data tables for Production and Development Apps

If you back up (save) an entire app to a .yaml file, it is relatively easy to extract the database schema (database structure definition), as Anvil sees it, as of the time of the backup, from that file. (Note: file format subject to change without notice!) This will at least give you a reliable reference, a checklist, so to speak, to use while you’re creating/modifying the tables and columns manually.

I agree, in the long run, more automation would be extremely helpful. Manual operations of this nature are notoriously error-prone. Automation should go both ways: write and read the schema.

1 Like

I miss database migrations. Some best practices from rails, or ecto in the elixir/phoenix world, and hard-won lessons about storing configuration in code in the drupal world are still challenging me here in anvil. Love the development speed, but the development model still has some maturing to do… continuous integration, continuous deployment, unit, integration, end-to-end testing, database migrations, there are a lot of things to tackle in between getting new applications built for clients with what’s already here. Love the little surprises that regularly show up in @meredydd 's newsletters, looking forward to growing with anvil, just feel the growing pains sometimes.

6 Likes

Any updates on this feature?

I just looked into this, and if you write yaml to directly create a db schema there is nothing in the yaml that can check this box in your app:
image

You can try it yourself, If you:

  1. Create a blank app with only a datatable
  2. Check the box in the picture above,( even change something to make sure all changes are saved)
  3. Export the yaml file
  4. Delete the app and re-import the same yaml from file.

Then the checkbox to auto-create missing columns will be unchecked on import.

This means you can’t even programmatically add a (nearly) blank DB schema and then have it auto-create the columns for you without using the anvil editor.
The reason I was looking into this was because of this question:

Here, @polat Is using the self hosted version, and is trying to load data without using the editor, but it seems like there are a few roadblocks to using the self hosting without having moderate database and data typing skills. (Even If you already have some data-science skills)

Maybe this could at least be possible to toggle on with the schema in the yaml ?
Since I think the code exists in the self-hosted version to auto-add columns? I’m not sure how someone would turn it on.

At this point I’m not even sure if I am asking for a feature or asking a question, I have thoroughly confused myself. :sweat_smile: