Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use alter_column in alembic?

I am writing a migration in alembic but seems impossible to me the change the value of server_defaults from something to nothing.

My code:

op.alter_column("foo", sa.Column("bar", sa.DateTime(timezone=False), server_default=None, nullable=True)) 

If i check after the migration the default is still NOW()

like image 565
tapioco123 Avatar asked Mar 24 '14 01:03

tapioco123


People also ask

What is op in alembic?

This file provides documentation on Alembic migration directives. The directives here are used within user-defined migration files, within the upgrade() and downgrade() functions, as well as any functions further invoked by those. All directives exist as methods on a class called Operations .

How do I change the datatype of a column in postgresql?

First, specify the name of the table to which the column you want to change belongs in the ALTER TABLE clause. Second, give the name of column whose data type will be changed in the ALTER COLUMN clause. Third, provide the new data type for the column after the TYPE keyword.

How do I rename a column in SQLAlchemy?

We can select a column with its alias name using the . label() method in SQLAlchemy. Sometimes we got the requirements to show a function result or column name with a different name, you can use the . label() method there.

How do I uninstall alembic?

You can do something like this: DELETE FROM alembic_version WHERE version_num='3aae6532b560'; INSERT INTO alembic_version VALUES ('3aae6532b560'); Above query could be done in one query by limiting number of deleted rows, but limiting within DELETE query is different between different databases engines.


2 Answers

To rename a column as of Alembic 0.9.5 I had to alter my migration to read as follows:

op.alter_column('my_table', 'old_col_name', nullable=False, new_column_name='new_col_name') 
like image 52
Iain Hunter Avatar answered Oct 01 '22 12:10

Iain Hunter


That's not how op.alter_column() works. Pass it arguments about what to change, not a new sa.Column instance.

op.alter_column('my_table', 'my_column', server_default=None) 
like image 37
davidism Avatar answered Oct 01 '22 12:10

davidism