2016-09-20 68 views
2

我有以下代碼PeeWee ORM報告說表已經存在

db = peewee.SqliteDatabase(":memory:") 

class CategoryProduct(peewee.Model): 
    category_code = peewee.CharField() 
    product_code  = peewee.CharField() 
    product_order = peewee.CharField() 
    default_category = peewee.CharField() 

    def __str__(self): 
     return "({0}, {1})".format(self.category_code, self.product_code) 

db.connect() 
db.create_tables([CategoryProduct]) 

當我運行該腳本,我得到

('CREATE TABLE "categoryproduct" ("id" INTEGER NOT NULL PRIMARY KEY, "category_code" VARCHAR(255) NOT NULL, "product_code" VARCHAR(255) NOT NULL, "product_order" VARCHAR(255) NOT NULL, "default_category" VARCHAR(255) NOT NULL)', []) 
Traceback (most recent call last): 
    File "/Users/daniels/Work/sandbox/venv/lib/python3.5/site-packages/peewee.py", line 3676, in execute_sql 
    cursor.execute(sql, params or()) 
sqlite3.OperationalError: table "categoryproduct" already exists 

任何想法是怎麼回事?如果我創建任何其他模塊它工作正常,但不知何故它不喜歡這個名字。

回答

2

的文檔有一張紙條給你:

注: 記住要指定在模型類的數據庫,否則peewee會回落到一個名爲「peewee.db」一個默認的SQLite數據庫。

只需將數據庫添加到您的類

class BaseModel(peewee.Model): 
    class Meta: 
     database = db 



class CategoryProduct(BaseModel): 
    category_code = peewee.CharField() 
    product_code  = peewee.CharField() 
    product_order = peewee.CharField() 
    default_category = peewee.CharField() 

    def __str__(self): 
     return "({0}, {1})".format(self.category_code, self.product_code) 
+0

噢,夥計...的複製和粘貼的問題...忘了還複製了「類元」 ......我真希望啓用調試模式也將顯示每個模型試圖使用的數據庫。本來更容易調試。 – daniels