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 app import app, db, User
|
||||||
from flask_migrate import upgrade
|
from flask_migrate import upgrade
|
||||||
|
from sqlalchemy import text
|
||||||
|
|
||||||
def init_db():
|
def init_db():
|
||||||
with app.app_context():
|
with app.app_context():
|
||||||
# Create all tables
|
try:
|
||||||
db.create_all()
|
# Try to run migrations first
|
||||||
# Run any pending migrations
|
|
||||||
upgrade()
|
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
|
# Create default admin user if it doesn't exist
|
||||||
admin = User.query.filter_by(username='admin').first()
|
admin = User.query.filter_by(username='admin').first()
|
||||||
|
|||||||
Reference in New Issue
Block a user