Create Data Tables with Code

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.

6 Likes

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

2 Likes

Has any headway been made on this feature?

1 Like

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.

4 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:

I came here looking for the same capability. (._.)

I’m working on it from within amoni

5 Likes

@owen.campbell I look forward to using amoni on second project (whatever it is). I can’t leverage it without first learning the Anvil platform itself (ongoing). I do miss vi(1), ptpython(1) and CLI friends (well,… for now I do occasionally do a git(1) pull + vi(1) + git(1) push + IDE refresh). But all good.

Have a look at

As a halfway house (which I use all the time in parallel with local development).

Is this feature still not implemented? It would be very nice to be able to dynamically create new tables with code, in order to keep the DB more organized.

Hi,

I was wondering if this is going to be added?

Something simple like: -
app_table.create_table(new_table)
app_table.drop_table(old_table)

Thanks
Haider

3 Likes

Will we ever see this feature make it? Would be nice to recreate tables with the same schema for each user on first login vs. doing it by hand.

See Views for a much more robust (and easier!) way to deal with per-user data.

1 Like

Appreciate the suggestion. My original intent was to use a separate table for each user (this is a very small scale application with few users). However, it seems like I will just have to change strategies and use one table with an additional user column.