Flask SQLalchemy - AttributeError: foreign_keys - PullRequest
0 голосов
/ 18 января 2020

Пока я пытаюсь выполнить свой первый запрос GET для остальных API, у меня есть эта трассировка. На данный момент там ничего не хранится в БД. Цель состоит в том, чтобы отобразить пустой ответ.

[2020-01-18 18:14:14,501] ERROR in app: Exception on /devices/ [GET]
Traceback (most recent call last):
  File "/home/debian/Documents/projects/server-restapi/env/lib/python3.7/site-packages/flask/app.py", line 2446, in wsgi_app
    response = self.full_dispatch_request()
  File "/home/debian/Documents/projects/server-restapi/env/lib/python3.7/site-packages/flask/app.py", line 1951, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/home/debian/Documents/projects/server-restapi/env/lib/python3.7/site-packages/flask/app.py", line 1820, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/home/debian/Documents/projects/server-restapi/env/lib/python3.7/site-packages/flask/_compat.py", line 39, in reraise
    raise value
  File "/home/debian/Documents/projects/server-restapi/env/lib/python3.7/site-packages/flask/app.py", line 1949, in full_dispatch_request
    rv = self.dispatch_request()
  File "/home/debian/Documents/projects/server-restapi/env/lib/python3.7/site-packages/flask/app.py", line 1935, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/debian/Documents/projects/server-restapi/api/routes/device.py", line 12, in get_list
    devices = Device.query.all()
  File "/home/debian/Documents/projects/server-restapi/env/lib/python3.7/site-packages/flask_sqlalchemy/__init__.py", line 512, in __get__
    mapper = orm.class_mapper(type)
  File "/home/debian/Documents/projects/server-restapi/env/lib/python3.7/site-packages/sqlalchemy/orm/base.py", line 441, in class_mapper
    mapper = _inspect_mapped_class(class_, configure=configure)
  File "/home/debian/Documents/projects/server-restapi/env/lib/python3.7/site-packages/sqlalchemy/orm/base.py", line 420, in _inspect_mapped_class
    mapper._configure_all()
  File "/home/debian/Documents/projects/server-restapi/env/lib/python3.7/site-packages/sqlalchemy/orm/mapper.py", line 1337, in _configure_all
    configure_mappers()
  File "/home/debian/Documents/projects/server-restapi/env/lib/python3.7/site-packages/sqlalchemy/orm/mapper.py", line 3229, in configure_mappers
    mapper._post_configure_properties()
  File "/home/debian/Documents/projects/server-restapi/env/lib/python3.7/site-packages/sqlalchemy/orm/mapper.py", line 1947, in _post_configure_properties
    prop.init()
  File "/home/debian/Documents/projects/server-restapi/env/lib/python3.7/site-packages/sqlalchemy/orm/interfaces.py", line 196, in init
    self.do_init()
  File "/home/debian/Documents/projects/server-restapi/env/lib/python3.7/site-packages/sqlalchemy/orm/relationships.py", line 1861, in do_init
    self._setup_join_conditions()
  File "/home/debian/Documents/projects/server-restapi/env/lib/python3.7/site-packages/sqlalchemy/orm/relationships.py", line 1941, in _setup_join_conditions
    can_be_synced_fn=self._columns_are_mapped,
  File "/home/debian/Documents/projects/server-restapi/env/lib/python3.7/site-packages/sqlalchemy/orm/relationships.py", line 2246, in __init__
    self._determine_joins()
  File "/home/debian/Documents/projects/server-restapi/env/lib/python3.7/site-packages/sqlalchemy/orm/relationships.py", line 2346, in _determine_joins
    consider_as_foreign_keys=consider_as_foreign_keys,
  File "<string>", line 2, in join_condition
  File "<string>", line 2, in _join_condition
  File "/home/debian/Documents/projects/server-restapi/env/lib/python3.7/site-packages/sqlalchemy/util/deprecations.py", line 130, in warned
    return fn(*args, **kwargs)
  File "/home/debian/Documents/projects/server-restapi/env/lib/python3.7/site-packages/sqlalchemy/sql/selectable.py", line 918, in _join_condition
    a, a_subset, b, consider_as_foreign_keys
  File "/home/debian/Documents/projects/server-restapi/env/lib/python3.7/site-packages/sqlalchemy/sql/selectable.py", line 972, in _joincond_scan_left_right
    b.foreign_keys, key=lambda fk: fk.parent._creation_order
  File "/home/debian/Documents/projects/server-restapi/env/lib/python3.7/site-packages/sqlalchemy/util/langhelpers.py", line 949, in __getattr__
    return self._fallback_getattr(key)
  File "/home/debian/Documents/projects/server-restapi/env/lib/python3.7/site-packages/sqlalchemy/util/langhelpers.py", line 923, in _fallback_getattr
    raise AttributeError(key)
AttributeError: foreign_keys
127.0.0.1 - - [18/Jan/2020 18:14:14] "GET /devices/ HTTP/1.1" 500 -

Упомянутые модели:


tags = db.Table('tag_device',
                db.Column('tag_id', db.Integer, db.ForeignKey('tag.id'), primary_key=True),
                db.Column('device_id', db.Integer, db.ForeignKey('device.id'), primary_key=True)
                )


class Device(db.Model):
    __tablename__ = 'device'
    id = db.Column(db.String(36), primary_key=True, autoincrement=False)
    entity_id = db.Column(db.Integer, db.ForeignKey('entity.id'), nullable=False)
    location_id = db.Column(db.Integer, db.ForeignKey('location.id'), nullable=False)
    deviceGroup_id = db.Column(db.Integer, db.ForeignKey('deviceGroup.id'), nullable=False)
    name = db.Column(db.String(50))
    layout_id = db.Column(db.Integer, db.ForeignKey('layout.id'), nullable=False)
    configuration = db.Column(db.Text)
    tags = db.relationship('Tag', secondary=tags, lazy='subquery', backref=db.backref('devices', lazy=True))
    is_active = db.Column(db.Boolean, default=True)
    created_at = db.Column(db.DateTime, default=datetime.utcnow)
    version = db.Column(db.SmallInteger, default=0)

    def __repr__(self):
        return '<Device %r>' % self.name


class Entity(db.Model):
    __tablename__ = 'entity'
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(255), nullable=False)
    devices = db.relationship('Device', backref='entity', lazy=True)
    deviceGroups = db.relationship('DeviceGroup', backref='entity', lazy=True)
    medias = db.relationship('Media', backref='entity', lazy=True)
    playlists = db.relationship('Playlist', backref='entity', lazy=True)
    tags = db.relationship('Tag', backref='entity', lazy=True)
    tagTypes = db.relationship('TagType', backref='entity', lazy=True)

    def __repr__(self):
        return '<Entity %r>' % self.name


class Location(db.Model):
    __tablename__ = 'location'
    id = db.Column(db.Integer, primary_key=True)
    continent = db.Column(db.String(4), nullable=True)
    country = db.Column(db.String(4), nullable=True)
    state = db.Column(db.String(40), nullable=True)
    address = db.Column(db.String(255), nullable=True)
    city = db.Column(db.String(255), nullable=True)
    zipcode = db.Column(db.String(255), nullable=True)
    name = db.Column(db.String(255), nullable=True)
    devices = db.relationship('Device', backref='location', lazy=True)

    def __repr__(self):
        return '<Location %r>' % self.name


class DeviceGroup(db.Model):
    __tablename__ = 'deviceGroup'
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(255), nullable=False)
    entity_id = db.Column(db.Integer, db.ForeignKey('entity.id'), nullable=False)
    tags = db.relationship('Tag', secondary=tags, lazy='subquery', backref=db.backref('deviceGroups', lazy=True))
    devices = db.relationship('Device', backref='device_group', lazy=True)
    children = db.relationship('DeviceGroup')
    parent_id = db.Column(db.Integer, db.ForeignKey('deviceGroup.id'))

    def __repr__(self):
        return '<DeviceGroup %r>' % self.name


class Layout(db.Model):
    __tablename__ = 'layout'
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(255), nullable=False)
    path = db.Column(db.String(255), nullable=False)
    devices = db.relationship('Device', backref='layout', lazy=True)

    def __repr__(self):
        return '<Layout %r>' % self.name


class TagType(db.Model):
    __tablename__ = 'tagType'
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(255))
    entity_id = db.Column(db.Integer, db.ForeignKey('entity.id'), nullable=False)
    tags = db.relationship('Tag', backref='tagType', lazy=True)


class Tag(db.Model):
    __tablename__ = 'tag'
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(255))
    entity_id = db.Column(db.Integer, db.ForeignKey('entity.id'), nullable=False)
    tagType_id = db.Column(db.Integer, db.ForeignKey('tagType.id'), nullable=False)

Для отношений один ко многим, ко многим ко многим я использовал официальный документация https://flask-sqlalchemy.palletsprojects.com/en/2.x/models/

Можете ли вы помочь мне разобраться в этой ошибке?

R

1 Ответ

0 голосов
/ 19 января 2020

Благодаря руководству @djnz я комментирую все отношения между объектами и раскомментирую один за другим. Одно из отношений «многие ко многим» не использовало таблицу хорошего соединения.

Спасибо за вашу помощь, R

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...