为什么sqlalchemy的默认列值不起作用 [英] Why isn't sqlalchemy's default column value working
本文介绍了为什么sqlalchemy的默认列值不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用Postgresql 9.1和SQLAlchemy 0.9。
I am using Postgresql 9.1 and SQLAlchemy 0.9.
问题是' default = 10 '
The problem is, 'default=10' doesn't work.
我的代码:
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")
我直接尝试了一条SQL语句,它看起来应该像这样:
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")
我在做什么错?
推荐答案
而不是使用default,而是使用server_default ...为我解决了该问题
https:// docs.sqlalchemy.org/en/latest/core/defaults.html?highlight=schema%20column#sqlalchemy.schema.ColumnDefault
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
这篇关于为什么sqlalchemy的默认列值不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文