Update init_db.py
This commit is contained in:
27
init_db.py
27
init_db.py
@@ -1,12 +1,33 @@
|
||||
from app import app, db, User
|
||||
from flask_migrate import upgrade
|
||||
from sqlalchemy import text
|
||||
|
||||
def init_db():
|
||||
with app.app_context():
|
||||
# Create all tables
|
||||
db.create_all()
|
||||
# Run any pending migrations
|
||||
try:
|
||||
# Try to run migrations first
|
||||
upgrade()
|
||||
except Exception as e:
|
||||
print(f"Migration error (this is normal if tables exist): {e}")
|
||||
|
||||
# Check if section_id column exists in plant table
|
||||
try:
|
||||
result = db.session.execute(text("SELECT section_id FROM plant LIMIT 1"))
|
||||
print("section_id column exists")
|
||||
except Exception:
|
||||
print("Adding section_id column to plant table")
|
||||
try:
|
||||
# Add section_id column if it doesn't exist
|
||||
db.session.execute(text("""
|
||||
ALTER TABLE plant
|
||||
ADD COLUMN IF NOT EXISTS section_id INTEGER
|
||||
REFERENCES section(id)
|
||||
"""))
|
||||
db.session.commit()
|
||||
print("Added section_id column successfully")
|
||||
except Exception as e:
|
||||
print(f"Error adding section_id column: {e}")
|
||||
db.session.rollback()
|
||||
|
||||
# Create default admin user if it doesn't exist
|
||||
admin = User.query.filter_by(username='admin').first()
|
||||
|
||||
Reference in New Issue
Block a user