document sequence + server default

Issue #3453 resolved
Gijs Molenaar
created an issue

When I supply a primary key a custom Sequence the sequence is created but not attached to the primary key column. Tested only with PostgreSQL and SQLAlchemy 1.0.5

example code

from sqlalchemy import Column,Integer, Sequence
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import create_engine

Base = declarative_base()


class TestCustomSeq(Base):
    __tablename__ = 'testseq'
    id = Column(Integer, Sequence('customseq'), primary_key=True)


alchemy_engine = create_engine('postgresql://gijs:gijs@localhost:5433/gijs', echo=True)
Base.metadata.create_all(alchemy_engine)

generated sql:

CREATE SEQUENCE customseq
COMMIT;
CREATE TABLE testseq (
    id INTEGER NOT NULL, 
    PRIMARY KEY (id)
);

Comments (6)

  1. Michael Bayer repo owner

    if you'd like to make the Sequence a server default, specify the SQL expression you'd like as server_default:

    from sqlalchemy import Column,Integer, Sequence
    from sqlalchemy.ext.declarative import declarative_base
    from sqlalchemy import create_engine
    
    Base = declarative_base()
    
    
    seq = Sequence('customseq')
    
    class TestCustomSeq(Base):
        __tablename__ = 'testseq'
        id = Column(Integer, seq, server_default=seq.next_value(), primary_key=True)
    
    
    alchemy_engine = create_engine('postgresql://scott:tiger@localhost/test', echo=True)
    Base.metadata.create_all(alchemy_engine)
    

    im -1 on changing anything here. at most just a documentation update.

  2. Log in to comment