初次提交
This commit is contained in:
33
backend/create_tables.py
Normal file
33
backend/create_tables.py
Normal file
@@ -0,0 +1,33 @@
|
||||
from sqlalchemy import create_engine, text
|
||||
from app.core.config import settings
|
||||
from app.core.database import Base
|
||||
from app.models import User
|
||||
|
||||
def create_user_table():
|
||||
try:
|
||||
print(f"连接数据库: {settings.DATABASE_URL}")
|
||||
|
||||
# 创建数据库引擎
|
||||
engine = create_engine(settings.DATABASE_URL, echo=True)
|
||||
|
||||
# 创建所有表
|
||||
Base.metadata.create_all(bind=engine)
|
||||
print("users表创建成功!")
|
||||
|
||||
# 检查表是否创建成功
|
||||
with engine.connect() as conn:
|
||||
result = conn.execute(text("DESCRIBE users"))
|
||||
columns = result.fetchall()
|
||||
print("\nusers表结构:")
|
||||
for column in columns:
|
||||
print(f" {column[0]}: {column[1]} {column[2]} {column[3]} {column[4]}")
|
||||
|
||||
print("\n数据库表创建完成!")
|
||||
return True
|
||||
|
||||
except Exception as e:
|
||||
print(f"创建表失败: {str(e)}")
|
||||
return False
|
||||
|
||||
if __name__ == "__main__":
|
||||
create_user_table()
|
||||
Reference in New Issue
Block a user