sqlalchemy and auto increments with postgresql

后端 未结 3 873
臣服心动
臣服心动 2021-01-13 15:37

I created a table with a primary key and a sequence but via the debug ad later looking at the table design, the sequence isn\'t applied, just created.

from s         


        
3条回答
  •  温柔的废话
    2021-01-13 16:12

    You specified an explicit Sequence() object with name. If you were to omit that, then SERIAL would be added to the id primary key specification:

    CREATE TABLE tramos (
        id INTEGER SERIAL NOT NULL, 
        nombre VARCHAR, 
        tramo_data VARCHAR, 
        estado BOOLEAN, 
        PRIMARY KEY (id)
    )
    

    A DEFAULT is only generated if the column is not a primary key.

    When inserting, SQLAlchemy will issue a select nextval(..) as needed to create a next value. See the PostgreSQL documentation for details.

提交回复
热议问题