daniels
daniels

Reputation: 19203

PeeWee ORM reports that table already exists

I have the following code

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])

And when I run the script I get

('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

Any idea what's going on? If I create any other module it works fine but somehow it doesn't like this name.

Upvotes: 2

Views: 1320

Answers (1)

Yoav Glazner
Yoav Glazner

Reputation: 8066

The docs have a note for you:

Note: Remember to specify a database on your model classes, otherwise peewee will fall back to a default sqlite database named “peewee.db”.

Just add the database to your class

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)

Upvotes: 2

Related Questions