python-3.x - PeeWee ORM 报告表已存在

标签 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

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

最佳答案

文档为您提供了注释:

注意: 请记住在模型类上指定一个数据库,否则 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)

关于python-3.x - PeeWee ORM 报告表已存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39591973/

相关文章:

python-3.x - 如何获取python3数据类实例的索引?

python - 为什么我在一个失败的命令后得到所有后续命令的 peewee.InternalError,使用 peewee ORM 和 posgresql?

python - on_delete ='CASCADE' 好像没有效果

python - 使用键值对更新 peewee 中的模型?

python - 如何使用 python 过滤 CSV 文件中两个日期之间的行并重定向到另一个文件?

python - 无法使用请求 HTML 找到 css 类

python - peewee.OperationalError : too many SQL variables on upsert of only 150 rows * 8 columns

mysql - 使用 peewee 连接 mysql 以及访问表的一些问题

mysql - 截断了不正确的 DOUBLE 值 mysql 存储过程

python-3.x - 为 Python 3.7.3 安装 GDAL 的问题