The function alembic.autogenerate.render_python_code doesn't work when you use server_default=func.current_timestamp()) in a column.
It used to work in alembic<=1.0.10 but it seems to be broken in any version since 1.0.11 up to 1.3.1. I'm using python 3.8, postgres 11.4 on ubuntu 18.10.
This is how to reproduce the problem:
from sqlalchemy import Table, Column, DateTime, func, MetaData, create_engine
from alembic.autogenerate import produce_migrations, render_python_code
from alembic.runtime.migration import MigrationContext
meta = MetaData()
engine = create_engine('postgresql+psycopg2://postgres:@localhost:32788/postgres')
c = Column('created_at', DateTime, server_default=func.current_timestamp())
t = Table('some_table', meta, c)
mc = MigrationContext.configure(engine.connect(), opts={'compare_type': True})
mig = produce_migrations(mc, meta)
python_code = render_python_code(mig.upgrade_ops, render_as_batch=False)
print(python_code)
This is the output on alembic <= 1.0.10:
# ### commands auto generated by Alembic - please adjust! ###
op.create_table('some_table',
sa.Column('created_at', sa.DateTime(), server_default=sa.text('CURRENT_TIMESTAMP'), nullable=True)
)
# ### end Alembic commands ###
And in alembic >= 1.0.11 it fails on this line:
python_code = render_python_code(mig.upgrade_ops, render_as_batch=False)
This is the output:
File ".virtualenv/lib/python3.8/site-packages/alembic/autogenerate/api.py", line 164, in render_python_code
render._render_cmd_body(up_or_down_op, autogen_context)
File ".virtualenv/lib/python3.8/site-packages/alembic/autogenerate/render.py", line 71, in _render_cmd_body
lines = render_op(autogen_context, op)
File ".virtualenv/lib/python3.8/site-packages/alembic/autogenerate/render.py", line 87, in render_op
lines = util.to_list(renderer(autogen_context, op))
File ".virtualenv/lib/python3.8/site-packages/alembic/autogenerate/render.py", line 172, in _add_table
for col in [
File ".virtualenv/lib/python3.8/site-packages/alembic/autogenerate/render.py", line 173, in <listcomp>
_render_column(col, autogen_context) for col in table.columns
File ".virtualenv/lib/python3.8/site-packages/alembic/autogenerate/render.py", line 601, in _render_column
rendered = _render_server_default(
File ".virtualenv/lib/python3.8/site-packages/alembic/autogenerate/render.py", line 650, in _render_server_default
return _render_potential_expr(
File ".virtualenv/lib/python3.8/site-packages/alembic/autogenerate/render.py", line 509, in _render_potential_expr
"sql": autogen_context.migration_context.impl.render_ddl_sql_expr(
AttributeError: 'NoneType' object has no attribute 'impl'
Not sure if I'm doing something wrong or if this was accidentally broken in release 1.0.11.
The function
alembic.autogenerate.render_python_codedoesn't work when you useserver_default=func.current_timestamp())in a column.It used to work in alembic<=1.0.10 but it seems to be broken in any version since 1.0.11 up to 1.3.1. I'm using python 3.8, postgres 11.4 on ubuntu 18.10.
This is how to reproduce the problem:
This is the output on alembic <= 1.0.10:
And in alembic >= 1.0.11 it fails on this line:
This is the output:
Not sure if I'm doing something wrong or if this was accidentally broken in release 1.0.11.