簡體   English   中英

Python Peewee沒有在postgres中創建唯一約束

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM