PeeWee ORM报告该表已存在

时间:2016-09-20 10:39:31

标签: python-3.x peewee

我有以下代码

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

知道发生了什么事吗?如果我创建任何其他模块,它工作正常但不知何故它不喜欢这个名称。

1 个答案:

答案 0 :(得分: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)