I started out with the Mega Flask tutorial to build a simple blog style website. I've used this in correlation with some python studying to try and reinforce what I've learned. In an effort to learn even more I decided to switch out the tutorials deprecated OAuth login for a traditional flask-login. I'm encountering an issue though when I try to login.
NoForeignKeysError: Could not determine join condition between parent/child tables on relationship User.posts - there are no foreign keys linking these tables. Ensure that referencing columns are associated with a ForeignKey or ForeignKeyConstraint, or specify a 'primaryjoin' expression.
With my limited understanding of Python, I've hit a pretty big roadblock here. I can't seem to figure out what I've done wrong.
Here's my models.py file.
followers = db.Table('followers',
db.Column('follower_id', db.Integer, db.ForeignKey('user.id')),
db.Column('followed_id', db.Integer, db.ForeignKey('user.id'))
)
class User(db.Model):
__tablename__ = "users"
id = db.Column(db.Integer, primary_key=True)
nickname = db.Column(db.String(64), index=True, unique=True)
email = db.Column(db.String(120), index=True, unique=True)
password = db.Column(db.String(100))
posts = db.relationship('Post', backref='author', lazy='dynamic')
about_me = db.Column(db.String(140))
last_seen = db.Column(db.DateTime)
followed = db.relationship('User',
secondary=followers,
primaryjoin=(followers.c.follower_id == id),
secondaryjoin=(followers.c.followed_id == id),
backref=db.backref('followers', lazy='dynamic'),
lazy='dynamic')
#reviews = db.relationship('Review', backref='author', lazy='dynamic') This is the review connectino for the user.
@staticmethod
def make_unique_nickname(nickname):
if User.query.filter_by(nickname=nickname).first() is None:
return nickname
version = 2
while True:
new_nickname = nickname + str(version)
if User.query.filter_by(nickname=new_nickname).first() is None:
break
version += 1
return new_nickname
def is_authenticated(self):
return True
def is_active(self):
return True
def is_anonymous(self):
return False
def get_id(self):
try:
return unicode(self.id) #python 2
except:
return str(self.id) #python 3
def follow(self, user):
if not self.is_following(user):
self.followed.append(user)
return self
def unfollow(self, user):
if self.is_following(user):
self.followed.remove(user)
return self
def is_following(self, user):
return self.followed.filter(
followers.c.followed_id == user.id).count() > 0
def followed_posts(self):
return Post.query.join(
followers, (followers.c.followed_id == Post.user_id)).filter(
followers.c.follower_id == self.id).order_by(
Post.timestamp.desc())
def __repr__(self):
return '<User {}>'.format(self.nickname)
class Post(db.Model):
id = db.Column(db.Integer, primary_key=True)
body = db.Column(db.String(140))
timestamp = db.Column(db.DateTime)
user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
def __repr__(self):
return '<Post {}>'.format(self.body)
and here's my views.py file that should contain relevant code:
#This loads the user from the database
@lm.user_loader
def load_user(id):
return User.query.get(int(id))
@app.before_request
def before_request():
g.user = current_user
if g.user.is_authenticated:
g.user.last_seen = datetime.utcnow()
db.session.add(g.user)
db.session.commit()
@app.errorhandler(404)
def not_found_error(error):
return render_template('404.html'), 404
@app.errorhandler(500)
def internal_error(error):
db.session.rollback()
return render_template('500.html'), 500
@app.route('/')
@app.route('/index')
@login_required
def index():
user = g.user
posts = [ #fake array of posts
{
'author': {'nickname': 'Zach'},
'body': 'Reviewed this product!'
},
{
'author': {'nickname': 'Mark'},
'body': 'I like buttcheese!'
}
]
return render_template('index.html',
title='Home',
user=user,
posts=posts)
@app.route('/login', methods=['GET', 'POST'])
def login():
if g.user is not None and g.user.is_authenticated:
return redirect(url_for('index'))
form = LoginForm()
if form.validate_on_submit():
user = User.query.get(form.nickname.data)
session['remember_me'] = form.remember_me.data
if user:
if check_password_hash(user.password, form.password.data):
user.authenticated = True
db.session.add(user)
db.session.commit()
login_user(user, remember=True)
flash("you've been logged in!, 'success'")
return redirect(url_for('index'))
else:
flash('your email or password doesnt match!', 'error')
return render_template('login.html',
title='Sign In',
form=form)
@app.route('/logout')
def logout():
logout_user()
return redirect(url_for('index'))
I'm really struggling with this because I don't understand DB relationships well enough yet. It's been a great learning experience, but if someone could help out that would be great.
I think this is a sqlalchemy error. When you define a relationship between User and Posts here:
posts = db.relationship('Post', backref='author', lazy='dynamic')
sqlalchemy needs to find a foreignkey on the database between user and posts. If you don't want to update your database schema you can define, like what you did on followed, a join that sqlalchemy can do to guess what are the posts related to the user.
In your case it might be:
posts = db.relationship('Post', backref='author', lazy='dynamic',
primaryjoin="User.id == Post.user_id")
Here is the documentation about it:
http://docs.sqlalchemy.org/en/latest/orm/join_conditions.html#specifying-alternate-join-conditions
Update:
I see that in fact Post.user_id has a foreign key defined. Maybe the sql server didn't create the foreign key? In any case what I said above may be a workaround.
Along with the post above I believe I have resolved the issue. I noticed that my tablename was actually "users" instead of "user". As a result, I needed to change all my foreign keys from user.id to users.id as it was looking for a table called "user" which didn't exist.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With