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?
Answers:
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
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?
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