Python Forum
Create SQLite3 database with peewee
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Create SQLite3 database with peewee
#1
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 :
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 :
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()
Please find below the trace back of the error :
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>
Reply
#2
The error you're encountering, peewee.ImproperlyConfigured: database attribute does not appear to be set on the model: <Model: User>, is due to the fact that you are trying to create tables using the create_tables function before setting the DATABASE variable to an instance of SqliteDatabase("test.db"). In your modified code, you are setting DATABASE to None initially, and then attempting to use it in the create_tables function before assigning it the SqliteDatabase instance.

To fix this issue, you need to set DATABASE to the SqliteDatabase("test.db") instance before calling the create_tables function. Here's the corrected code:

python
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()
Now, DATABASE is set to the SqliteDatabase("test.db") instance before calling create_tables(), which should resolve the issue you're facing.

link removed is a link to a top-notch learning platform.
buran write Dec-20-2023, 02:33 PM:
Please, use proper tags when post code, traceback, output, etc. This time I have added tags for you.
See BBcode help for more info.
Larz60+ write Dec-20-2023, 03:49 AM:
Links removed
Reply
#3
(Dec-19-2023, 12:03 PM)khanzain Wrote: which should resolve the issue you're facing.
I don't see any changes between your code and OP second snippet
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Regex replace in SQLite3 database WJSwan 1 814 Dec-04-2023, 05:55 PM
Last Post: Larz60+
  Rows not adding to sqlite3 database using SQLAlchemy Calab 11 1,734 Jun-02-2023, 05:53 PM
Last Post: bowlofred
  Basic SQL query using Py: Inserting or querying sqlite3 database not returning data marlonbown 3 1,393 Nov-08-2022, 07:16 PM
Last Post: marlonbown
  Python Variables and Sqlite3 Database Staples200 1 3,173 May-25-2021, 02:40 AM
Last Post: Staples200
  sqlite3 database problem Maryan 2 2,494 Oct-05-2020, 05:21 AM
Last Post: buran
  sqlite3 database does not save data across restarting the program SheeppOSU 1 3,459 Jul-24-2020, 05:53 AM
Last Post: SheeppOSU
  ZIP file in Sqlite3 database chesschaser 4 3,508 Jul-23-2020, 09:53 PM
Last Post: chesschaser
  Trying to send input from html form to database SQLite3 RonnyGiezen 3 71,981 Dec-21-2019, 02:09 PM
Last Post: ibreeden
  Create application that will take information from database and insert into PDF sorrelsj 1 2,081 Aug-19-2019, 10:08 PM
Last Post: Gribouillis
  display the contents of a sqlite3 database according to the dates atlass218 4 2,985 Mar-03-2019, 06:43 AM
Last Post: atlass218

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020