Lerndatenbank/lernplattform/models.py

24 lines
1.1 KiB
Python
Raw Normal View History

2024-08-05 20:43:29 +02:00
from lernplattform import db
# Define a User model that represents the user table in the database
2024-08-05 20:43:29 +02:00
class User(db.Model):
# 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)
# 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)
# 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)
# 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)
# 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}')"
# 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
# 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()