Answer the question
In order to leave comments, you need to log in
Flask-SQLAlchemy error on join?
I'm trying to join 2 tables in Flask-SQLAlchemy. An attribute error appears. What am I doing wrong?
Models:
class Profile(db.Model):
"""User profiles table"""
user_id = db.Column(db.Integer, primary_key=True, autoincrement=False)
selected_hero = db.Column(db.Integer)
heroes = db.Column(db.PickleType)
hero = db.relationship('Hero', backref='profile', lazy='dynamic')
class Hero(db.Model):
"""Heroes table"""
hero_id = db.Column(db.Integer, db.ForeignKey('profile.user_id'), primary_key=True, autoincrement=True)
name = db.Column(db.Text)
hp = db.Column(db.Integer)
quer = models.Profile.query\
.join(models.Hero)\
.filter_by(user_id=ev.a_id).first()
VkMethods.messages_send(f'Test\nname: {quer.name}\nhp: {quer.hp}', ev.conf)
File "main.py", line 83, in db_test
VkMethods.messages_send(f'Test\nname: {quer.name}\nhp: {quer.hp}', ev.conf)
AttributeError: 'NoneType' object has no attribute 'name'
2020-06-13 00:14:56,783: Exception on / [POST]
Traceback (most recent call last):
File "/usr/lib/python3.7/site-packages/sqlalchemy/orm/base.py", line 399, in _entity_descriptor
return getattr(entity, key)
AttributeError: type object 'Hero' has no attribute 'user_id'
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question