2024-08-05 20:43:29 +02:00
|
|
|
from lernplattform import db
|
|
|
|
|
2024-08-06 19:06:09 +02:00
|
|
|
# Define a User model that represents the user table in the database
|
2024-08-05 20:43:29 +02:00
|
|
|
class User(db.Model):
|
2024-08-06 19:06:09 +02:00
|
|
|
# Primary key column for the table, auto-incrementing integer
|
2024-08-05 20:43:29 +02:00
|
|
|
id = db.Column(db.Integer, primary_key=True)
|
2024-08-06 19:06:09 +02:00
|
|
|
# Username column with a maximum length of 20 characters, unique and not nullable
|
2024-08-05 20:43:29 +02:00
|
|
|
username = db.Column(db.String(20), unique=True, nullable=False)
|
2024-08-06 19:06:09 +02:00
|
|
|
# Email column with a maximum length of 120 characters, unique and not nullable
|
2024-08-05 20:43:29 +02:00
|
|
|
email = db.Column(db.String(120), unique=True, nullable=False)
|
2024-08-06 19:06:09 +02:00
|
|
|
# Password column with a maximum length of 60 characters, not nullable
|
2024-08-05 20:43:29 +02:00
|
|
|
password = db.Column(db.String(60), nullable=False)
|
|
|
|
|
2024-08-06 19:06:09 +02:00
|
|
|
# Magic method to represent the object as a string when printed or used in string context
|
2024-08-05 20:43:29 +02:00
|
|
|
def __repr__(self):
|
|
|
|
return f"User('{self.username}', '{self.email}')"
|
|
|
|
|
2024-08-06 19:06:09 +02:00
|
|
|
# Function to initialize the database by creating all tables defined in models if they do not exist
|
2024-08-05 20:43:29 +02:00
|
|
|
def initialize_database():
|
|
|
|
from lernplattform import app
|
2024-08-06 19:06:09 +02:00
|
|
|
# Create an application context before running operations on the database within this function
|
2024-08-05 20:43:29 +02:00
|
|
|
with app.app_context():
|
|
|
|
db.create_all()
|