[英]Python Peewee not creating unique constraints in postgres
我通过使用以下代码片段来创建模式:
from peewee import *
db = PostgresqlDatabase('db', user='user', password='pass', host="localhost")
class Actor(Model):
name = TextField(unique=True)
db.create_table(Actor)
不幸的是,peewee似乎没有添加UNIQUE
约束。
输出SQL:
-- Table: actor
-- DROP TABLE actor;
CREATE TABLE actor
(
id serial NOT NULL,
name text NOT NULL,
CONSTRAINT actor_pkey PRIMARY KEY (id)
)
WITH (
OIDS=FALSE
);
ALTER TABLE actor
OWNER TO user;
实际上,我可以添加多个具有相同name
行。
有什么问题的想法吗? 提前非常感谢大家:)
如果这很重要,我正在使用postgres 9.3.10 ... python 2.7.6,peewee 2.6.4(尽管尝试了不同的版本)。
您应该使用Model.create_table()
API,该API处理创建表和索引。
另外,您可以调用db.create_tables()
,它可以解析模型依赖性并以适当的顺序创建多个表(和索引)。
db.create_table()
您正在使用的API db.create_table()
只是创建表本身-没有索引。
所以:
db.create_tables([Actor])
要么:
Actor.create_table()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.