L
L
lightmanLP2020-06-13 03:45:33
Flask
lightmanLP, 2020-06-13 03:45:33

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)


Request:
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)


Mistake:
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'


PS The data in the tables are present.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dimonchik, 2020-06-13
@dimonchik2013

.filter_by( hero_id =ev.a_id).first()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question