coleifer / peewee

a small, expressive orm -- supports postgresql, mysql, sqlite and cockroachdb
http://docs.peewee-orm.com/
MIT License
11.06k stars 1.37k forks source link

create_tables() does not create any fields #2912

Closed PhilippCh closed 2 months ago

PhilippCh commented 2 months ago

Hi there!

New to python in general, so please excuse it if I'm doing something wrong, but I cannot figure out why create_tables([Task]) does not create the fields that I provided for the Task class.

import datetime

from peewee import Model, TextField, BooleanField, DateTimeField
from playhouse.shortcuts import model_to_dict
from playhouse.sqlite_ext import SqliteExtDatabase

db = SqliteExtDatabase('shoppr.db', pragmas={
    'journal_mode': 'wal',  # WAL-mode.
    'cache_size': -64 * 1000,  # 64MB cache.
    'synchronous': 0})

class Task(Model):
    title: TextField()
    description: TextField()
    createdAt = DateTimeField(default=datetime.datetime.now)
    changedAt = DateTimeField(default=datetime.datetime.now)
    isDone: BooleanField(default=False)

    class Meta:
        database = db

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

All that is created from this in the SQLite db is the id column: image

When debugging into create_tables(), it appears that the model is missing some fields (title, description, isDone) while others (createdAt, changedAt) are present, but not created in the DB. image

Is there anything I'm doing wrong? Your help would be greatly appreciated!

Seluj78 commented 2 months ago

You are using : instead of = :)

Try

import datetime

from peewee import Model, TextField, BooleanField, DateTimeField
from playhouse.shortcuts import model_to_dict
from playhouse.sqlite_ext import SqliteExtDatabase

db = SqliteExtDatabase('shoppr.db', pragmas={
    'journal_mode': 'wal',  # WAL-mode.
    'cache_size': -64 * 1000,  # 64MB cache.
    'synchronous': 0})

class Task(Model):
    title = TextField()
    description = TextField()
    createdAt = DateTimeField(default=datetime.datetime.now)
    changedAt = DateTimeField(default=datetime.datetime.now)
    isDone = BooleanField(default=False)

    class Meta:
        database = db

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

This gives me Screenshot 2024-07-08 at 12 22 21

PhilippCh commented 2 months ago

Oh wow, thanks a lot... like I said, still getting used to python 😀