Lerndatenbank/lernplattform/models.py
Manuel Weiser 751c67c05b File for classes created.
class to check for logged in user created
2024-08-06 19:06:09 +02:00

24 lines
1.1 KiB
Python

from lernplattform import db
# Define a User model that represents the user table in the database
class User(db.Model):
# Primary key column for the table, auto-incrementing integer
id = db.Column(db.Integer, primary_key=True)
# Username column with a maximum length of 20 characters, unique and not nullable
username = db.Column(db.String(20), unique=True, nullable=False)
# Email column with a maximum length of 120 characters, unique and not nullable
email = db.Column(db.String(120), unique=True, nullable=False)
# Password column with a maximum length of 60 characters, not nullable
password = db.Column(db.String(60), nullable=False)
# Magic method to represent the object as a string when printed or used in string context
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
def initialize_database():
from lernplattform import app
# Create an application context before running operations on the database within this function
with app.app_context():
db.create_all()