Incorrect Postgres sequences

Issue #1563 resolved
Former user created an issue

SQLAlchemy must be attach sequence to field

Example:

>>> __import__('sqlalchemy').__version__
'0.5.6'
>>> from sqlalchemy import create_engine, MetaData, Table, Column, Integer, Sequence
>>> engine = create_engine('postgres://test@localhost/test', echo=True)
>>> metadata = MetaData()
>>> temp_table = Table('temp', metadata,
...     Column('id', Integer,Sequence('test_id_seq'), primary_key=True)
... )
>>> metadata.create_all(engine)
...
2009-10-06 17:49:09,257 INFO sqlalchemy.engine.base.Engine.0x...b22c CREATE SEQUENCE test_id_seq
...
CREATE TABLE temp (
        id INTEGER NOT NULL,
        PRIMARY KEY (id)
)
...

Table SQL must be

CREATE TABLE temp (
        id SERIAL,
        PRIMARY KEY (id)
)

or

CREATE SEQUENCE test_id_seq;
CREATE TABLE temp (
        id integer DEFAULT nextval('test_id_seq') NOT NULL,
        PRIMARY KEY (id)
);

Comments (2)

  1. Mike Bayer repo owner

    dont use Sequence() - the SERIAL datatype will be used instead. If you'd like Sequence() to be present such that the table works on Oracle/Firebird as well, add optional=True to the Sequence. Documented here: http://www.sqlalchemy.org/docs/05/metadata.html#defining-sequences

    Alternatively you can specify server_default=text("nextval('test_id_seq')") to get the exact DEFAULT clause you're looking for there but the automatic approach is easier.

  2. Log in to comment