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


Messages In This Thread
Create SQLite3 database with peewee - by Jim53_1980 - Dec-17-2023, 04:08 PM
RE: Create SQLite3 database with peewee - by buran - Dec-20-2023, 02:38 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Regex replace in SQLite3 database WJSwan 1 877 Dec-04-2023, 05:55 PM
Last Post: Larz60+
  Rows not adding to sqlite3 database using SQLAlchemy Calab 11 1,939 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,503 Nov-08-2022, 07:16 PM
Last Post: marlonbown
  Python Variables and Sqlite3 Database Staples200 1 3,275 May-25-2021, 02:40 AM
Last Post: Staples200
  sqlite3 database problem Maryan 2 2,579 Oct-05-2020, 05:21 AM
Last Post: buran
  sqlite3 database does not save data across restarting the program SheeppOSU 1 3,535 Jul-24-2020, 05:53 AM
Last Post: SheeppOSU
  ZIP file in Sqlite3 database chesschaser 4 3,619 Jul-23-2020, 09:53 PM
Last Post: chesschaser
  Trying to send input from html form to database SQLite3 RonnyGiezen 3 86,759 Dec-21-2019, 02:09 PM
Last Post: ibreeden
  Create application that will take information from database and insert into PDF sorrelsj 1 2,129 Aug-19-2019, 10:08 PM
Last Post: Gribouillis
  display the contents of a sqlite3 database according to the dates atlass218 4 3,080 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