Why isn't sqlalchemy's default column value working

Question:

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?

Asked By: Aylwyn Lake

||

Answers:

Answered By: tbischel
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.