On Jan 30, 2014, at 11:42 AM, [email protected] wrote: > I'm on my last hurdle with implementing Alembic with our SqlAlchemy models, > and the issue is I can't get Alembic to implement my id column with > BIGSERIAL, it always defaults to SERIAL. I implemented a TypeDecorator on > each of the primary key columns that I want to be BIGSERIAL, the > implementation is below. Note that I need to work with SQLLITE for in memory > tests that run which is why i'm using a decorator. From looking at the > source code in base.py for postgres, this should work. However, when I set a > print statement in the source where you do check (as below), it appears that > the impl_type is BIGINT and falls through to SERIAL. > > if isinstance(impl_type, sqltypes.BigInteger): > colspec += " BIGSERIAL" > elif isinstance(impl_type, sqltypes.SmallInteger): > colspec += " SMALLSERIAL" > else: > colspec += " SERIAL" > > > > from sqlalchemy.types import ( > TypeDecorator, CHAR, Numeric, Float, > Integer, BigInteger) > > class SafeBigInteger(Types.TypeDecorator): > impl = BigInteger > > def load_dialect_impl(self, dialect): > if dialect.name == 'postgresql': > return dialect.type_descriptor(BigInteger) > else: > return dialect.type_descriptor(Integer)
for BIGSERIAL you use BigInteger directly. Using “print” by itself for any SQL DDL or statement is not going to give you what you want because “print” does not specify the dialect as Postgresql, you’d need to call compile(dialect=postgresql.dialect()) to get a PG-specific compilation. Also, to use different types on different backends, you don’t need TypeDecorator, use with_variant(): type = BigInteger.with_variant(Integer, “sqlite”) http://docs.sqlalchemy.org/en/rel_0_9/core/types.html?highlight=with_variant#sqlalchemy.types.TypeEngine.with_variant BIGINT is a subclass of BigInteger so will pass the check as isinstance() of BigInteger.
signature.asc
Description: Message signed with OpenPGP using GPGMail
