33

I am using Postgresql 9.1 and SQLAlchemy 0.9.

The problem is, 'default=10' doesn't work.

My Code:

conn_str = 'postgresql://test:pass@localhost/test'
engine = create_engine(conn_str)
metadata = MetaData(bind=engine)

cols=[]
cols += [Column('Name', VARCHAR(20), primary_key=True, nullable=False)]
cols += [Column('age', INT, default=10, nullable=False )]
Table('t1', metadata, *cols)
metadata.create_all(engine)

psql:

test=> \dS t1
            Table "public.t1"
Column |         Type          | Modifiers
--------+-----------------------+-----------
Name   | character varying(20) | not null
age    | integer               | not null
Indexes:
    "t1_pkey" PRIMARY KEY, btree ("Name")

I tried with an SQL statement directly and it should look like:

test=> \dS t2
              Table "public.t2"
 Column |         Type          | Modifiers
--------+-----------------------+------------
 Name   | character varying(10) | not null
 age    | integer               | default 10
Indexes:
    "t2_pkey" PRIMARY KEY, btree ("Name")

What am I doing wrong?

1
  • 3
    I have the same issues. All default values are being ignored. Commented Apr 13, 2016 at 13:32

1 Answer 1

39

instead of using default, use server_default... fixed the problem for me https://docs.sqlalchemy.org/en/latest/core/defaults.html?highlight=schema%20column#sqlalchemy.schema.ColumnDefault

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.