Dec-17-2023, 04:08 PM
Hello everybody,
I begin with peewee to create SQLite database. When I use the python code example supplied with the documentation, all works correctly but when I do some minor changes (from my point of view), the database is created but an error occurs and the tables are not cretaed.
Peewee original code example :
Similar code with minor change :
Please find below the trace back of the error :
I begin with peewee to create SQLite database. When I use the python code example supplied with the documentation, all works correctly but when I do some minor changes (from my point of view), the database is created but an error occurs and the tables are not cretaed.
Peewee original code example :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
from peewee import * # create a peewee database instance -- our models will use this database to # persist information DATABASE = SqliteDatabase( "test.db" ) def create_tables(): with DATABASE as db: db.create_tables([User, Relationship, Message]) # model definitions -- the standard "pattern" is to define a base model class # that specifies which database to use. then, any subclasses will automatically # use the correct storage. class BaseModel(Model): class Meta: database = DATABASE # the user model specifies its fields (or columns) declaratively, like django class User(BaseModel): username = CharField(unique = True ) password = CharField() email = CharField() join_date = DateTimeField() # this model contains two foreign keys to user -- it essentially allows us to # model a "many-to-many" relationship between users. by querying and joining # on different columns we can expose who a user is "related to" and who is # "related to" a given user class Relationship(BaseModel): from_user = ForeignKeyField(User, backref = 'relationships' ) to_user = ForeignKeyField(User, backref = 'related_to' ) class Meta: # `indexes` is a tuple of 2-tuples, where the 2-tuples are # a tuple of column names to index and a boolean indicating # whether the index is unique or not. indexes = ( # Specify a unique multi-column index on from/to-user. (( 'from_user' , 'to_user' ), True ), ) # a dead simple one-to-many relationship: one user has 0..n messages, exposed by # the foreign key. a users messages will be accessible as a special attribute, # User.messages. class Message(BaseModel): user = ForeignKeyField(User, backref = 'messages' ) content = TextField() pub_date = DateTimeField() if __name__ = = "__main__" : create_tables() |
Similar code with minor change :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
from peewee import * # create a peewee database instance -- our models will use this database to # persist information DATABASE = None def create_tables(): with DATABASE as db: db.create_tables([User, Relationship, Message]) # model definitions -- the standard "pattern" is to define a base model class # that specifies which database to use. then, any subclasses will automatically # use the correct storage. class BaseModel(Model): class Meta: database = DATABASE # the user model specifies its fields (or columns) declaratively, like django class User(BaseModel): username = CharField(unique = True ) password = CharField() email = CharField() join_date = DateTimeField() # this model contains two foreign keys to user -- it essentially allows us to # model a "many-to-many" relationship between users. by querying and joining # on different columns we can expose who a user is "related to" and who is # "related to" a given user class Relationship(BaseModel): from_user = ForeignKeyField(User, backref = 'relationships' ) to_user = ForeignKeyField(User, backref = 'related_to' ) class Meta: # `indexes` is a tuple of 2-tuples, where the 2-tuples are # a tuple of column names to index and a boolean indicating # whether the index is unique or not. indexes = ( # Specify a unique multi-column index on from/to-user. (( 'from_user' , 'to_user' ), True ), ) # a dead simple one-to-many relationship: one user has 0..n messages, exposed by # the foreign key. a users messages will be accessible as a special attribute, # User.messages. class Message(BaseModel): user = ForeignKeyField(User, backref = 'messages' ) content = TextField() pub_date = DateTimeField() if __name__ = = "__main__" : DATABASE = SqliteDatabase( "test.db" ) create_tables() |
Error:Traceback (most recent call last):
File "c:\Users\Dell\Documents\Jean-Marie\supaiment\server_app\dbsupaiement.py", line 53, in <module>
create_tables()
File "c:\Users\Dell\Documents\Jean-Marie\supaiment\server_app\dbsupaiement.py", line 9, in create_tables
db.create_tables([User, Relationship, Message])
File "C:\Users\Dell\AppData\Local\Programs\Python\Python311\Lib\site-packages\peewee.py", line 3441, in create_tables
model.create_table(**options)
File "C:\Users\Dell\AppData\Local\Programs\Python\Python311\Lib\site-packages\peewee.py", line 6943, in create_table
if safe and not cls._schema.database.safe_create_index \
^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Dell\AppData\Local\Programs\Python\Python311\Lib\site-packages\peewee.py", line 5832, in database
raise ImproperlyConfigured('database attribute does not appear to '
peewee.ImproperlyConfigured: database attribute does not appear to be set on the model: <Model: User>