Data Table: Add Column without Adding Row?

I feel like this is one of those things I assume should be really easy, but for solid reasons it’s not (or maybe I’m just missing something obvious):

I’m trying to add a column to an app table through code. I know I can do this when adding a row by assigning a value to a column that doesn’t exist, but Ideally I would like to add a column without needing to create a row. Is there a way to do that?

well you could always add a row with the new column and then immediately delete the row

Not a bad idea, I was trying to go a much more complicated route, but will do that instead.

Any reason why there wouldn’t be an .add_column method for app tables?

I’m not speaking for the Anvil team, they only know that reason, this is just my personal opinion:

  • Adding columns from code is a bad idea.
  • I would ask another question: “why do they allow to automatically create columns when they don’t exist?”

If the name of the column is unknown at app design time, then the column name should be part of the data, not part of the database structure.

A common approach is to store both column name and list of values in a simple object column. Simple object columns can’t store certain data types, like media or date, so this approach has some limits. But even when those limits hurt, there usually is a better way to store the two pieces of info (column_name, list_of_values), other than column proliferation.

Unless you’re trying to automate database structure comparisons and updates (data migrations).

But yes, that should be a rare, special case.

1 Like

As suspected, there are some good reasons hah. I’m not doing anything that complex, so I suspect i’ve just setup my data table in a less than optimal way.