i've been scratching my head for a bit of time now with this error and I can't seem to figure out what's wrong. Maybe you can help?
Using Flask-Migrate i've modified my models and am attempting to migrate the database accordingly.
The error i'm running into seems to be between two specific models User
and Transaction
.
After running
$ python run.py db init
$ python run.py db migrate
$ python run.py db upgrade
I recieve this error:
sqlalchemy.exc.OperationalError: (_mysql_exceptions.OperationalError) (1825, "Failed to add the foreign key constraint on table 'transaction_table'. Incorrect options in FOREIGN KEY constraint 'my_database/transaction_table_ibfk_1'") [SQL: 'ALTER TABLE transaction_table ADD FOREIGN KEY(user_id) REFERENCES users (id)']
Here is how i've set up my models note the tablename override:
class User(db.Model):
__tablename__ = 'users'
id = db.Column('id', db.Integer, primary_key=True)
email = db.Column(db.String(120), unique=True, index=True)
password = db.Column('password', db.String(10))
role = db.Column('role', db.String(3))
registered_on = db.Column('registered_on', db.DateTime)
transactions = db.relationship('transaction_table', backref=db.backref('user', lazy='joined'), lazy='dynamic')
class Transaction(db.Model):
__tablename__ = 'transaction_table'
id = db.Column(db.Integer, primary_key=True)
user_id = db.Column(db.Integer, db.ForeignKey('users.id'))
transaction_date = db.Column('transaction_date', db.DateTime)
requested_donor_id = db.Column(db.Integer, db.ForeignKey('donor_db.id'))
I've used this guide for setting up my models with Foreign Keys and relationships.
I've used this Blog Post to help diagnose my problems to no avail. Might you know what i'm missing?
Thanks in advance.