Files
ds_zagres_ai/app/database/db.py
T

37 lines
1.0 KiB
Python
Raw Normal View History

2025-05-09 15:41:16 +01:00
"""
Database module for the application.
"""
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy import MetaData
# Define naming convention for constraints
convention = {
"ix": 'ix_%(column_0_label)s',
"uq": "uq_%(table_name)s_%(column_0_name)s",
"ck": "ck_%(table_name)s_%(constraint_name)s",
"fk": "fk_%(table_name)s_%(column_0_name)s_%(referred_table_name)s",
"pk": "pk_%(table_name)s"
}
# Create SQLAlchemy instance with naming convention
db = SQLAlchemy(metadata=MetaData(naming_convention=convention))
def init_app(app):
"""
Initialize the database with the Flask application.
Args:
app: Flask application instance.
"""
db.init_app(app)
# Only initialize database if configured to do so
if app.config.get('INITIALIZE_DATABASE', False):
# Import models to ensure they are registered with SQLAlchemy
from app.models import user, chat, document
# Create tables if they don't exist
with app.app_context():
db.create_all()