[Sqlalchemy-tickets] Issue #3951: PostgreSQL Sequence not created by metadata.create_all() from dec
Brought to you by:
zzzeek
From: Andrew P. R. <iss...@bi...> - 2017-03-31 21:35:39
|
New issue 3951: PostgreSQL Sequence not created by metadata.create_all() from declarative mixin https://bitbucket.org/zzzeek/sqlalchemy/issues/3951/postgresql-sequence-not-created-by Andrew Paulo Robillo: I'd been copying this [snippet](https://bitbucket.org/snippets/shimofuri/bALAz) from my old cookbook to add a surrogate key to a declarative table class: ``` #!python class MixinIntKey(object): @declared_attr def key(cls): return Column( pg.INTEGER, Sequence(f"sqk_{cls.__tablename__}", 1, 1), nullable=False, primary_key=True ) ``` This code was working before (some 3-4 years ago, I think around version 0.8 ): it generates the Sequence when I build the database objects through metadata.create_all(). The only change I made to the snippet was to update it to use format strings (Python 3.6). After some googling around and testing (see attached file/[link](https://bitbucket.org/snippets/shimofuri/bALAz)), the Sequence can be generated only if the metadata is passed during Sequence init: ``` #!python Sequence(f"sqk_{cls.__tablename__}", 1, 1, metadata=metadata) ``` My environment: psycopg2==2.7.1, SQLAlchemy==1.1.8, Python 3.6 64bit on Windows 8.1 64bit, PostgreSQL 9.6 server |