Files
aiagent/saars/backend/scripts/create_tables.py
2026-03-07 10:29:17 +08:00

34 lines
953 B
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env python3
"""
用 SQLAlchemy 建表(不依赖 Flask-Migrate。适用于首次部署或 migrations 未就绪时。
用法: 在 backend 目录下设置 DATABASE_URL 后执行
python scripts/create_tables.py
或: docker compose run --rm -e DATABASE_URL=... backend python scripts/create_tables.py
"""
import os
import sys
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
os.environ.setdefault("FLASK_ENV", "development")
from app import create_app, db
from app.models.user import User, Role
from app.models.chat import Conversation, Message
from app.utils.ensure_admin import ensure_admin_user
def main():
app = create_app()
with app.app_context():
print("创建表...")
db.create_all()
print("表已创建。")
print("创建默认管理员 (admin / 123456)...")
ensure_admin_user()
print("完成。")
if __name__ == "__main__":
main()