document sequence + server default

Issue #3453 resolved
Former user 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. Mike 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. Former user Account Deleted reporter

    sure, I just didn't knew how to get this going but this results in what I want to accomplish! thanks.

  3. Former user Account Deleted reporter

    btw, if you supply optional=True to sequence it does t work, but the name is ignored. That is why I thought the usage was a bit inconsistent

  4. Log in to comment