0

I use sqlalchemy 1.4 and alembic for migrations.

Previously, my column type looked like this:

has_bubble_in_countries = sa.Column((ARRAY(sa.Enum(Country))), nullable=False, default=[])

Which was not allowing me to add or remove any elements from this array.

Then, I made it immutable by changing it like this:

has_bubble_in_countries = sa.Column(MutableList.as_mutable(ARRAY(sa.Enum(Country))), nullable=False, default=[])

Does this change require a migration? If so, what is the alembic's property in order to detect this type of change?

My first thought was that this is not an altering of the type so I considered that migration is not needed.

Razvan
  • 143
  • 8

2 Answers2

1

Found the answer. The change from a mutable sqlalchemy ARRAY type to an immutable ARRAY is just a Python behavioral change and not a change in the schema of the database, so no migration is needed.

Razvan
  • 143
  • 8
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 25 '23 at 09:50
0

The answer to this is "it depends".

If you are using a database that supports the ALTER COLUMN TYPE command, then you can change the type of a column without needing to create a new column and copy the data over. However, if you are using a database that does not support that command, then you will need to create a new column and copy the data over.

If you are using a database that does not support ALTER COLUMN TYPE, then you can use the postgresql_alter_column_type operation in Alembic to do this. This will create a new column, copy the data over, and then drop the old column. You can read more about it here:

https://alembic.sqlalchemy.org/en/latest/ops.html#alembic.operations.Operations.postgresql_alter_column_type

PCDSandwichMan
  • 1,964
  • 1
  • 12
  • 23
  • My main dilemma is if this change from making the column immutable to being mutable at first requires a migration. Is it a change of type from a mutable array to an immutable one? – Razvan Jan 22 '23 at 22:11
  • I think you need to create a new migration for this change. I'm not sure if alembic can detect this change automatically. You can try to run `alembic revision --autogenerate` and see if it detects the change. If it doesn't, you can create a new migration manually. – PCDSandwichMan Jan 22 '23 at 22:13
  • Already tried that. The change was not detected. – Razvan Jan 22 '23 at 22:34