通过SQLAlchemy插入对象时发生AttributeError:'int'对象没有属性'_sa_instance_state'

时间:2019-01-11 20:38:02

标签: python flask-sqlalchemy

我正在通过API请求数据,并将这些JSON数据存储在变量中。目标是使用SQLAlchemy(Python,Flask)将这些数据存储在MySQL数据库中。

当前负载有多个停止点,每个停止点可以有多个动作。在session.commit()之后,tour_stop_to_add被插入到数据库中,并为其分配了ID。

因此,tour_stop_to_add的ID可用。此外,该类型已成功转换为请求的枚举,并且transportOrderNumber也可以作为字符串使用。

模型的数据类型与为对象创建提供的类型等效。存储在“操作”中的字典值是可用的。

停停和动作类

class TourStop(db.Model):
    __tablename__ = "tour_stop"

    id = db.Column(db.Integer, primary_key=True)

    actions = db.relationship("TourStopAction", backref="tour_stop", lazy=False)


class ActionType(Enum):
    PICKUP = 'PICKUP'
    DELIVERY = 'DELIVERY'


class TourStopAction(db.Model):
    __tablename__ = "tour_stop_action"

    id = db.Column(db.Integer, primary_key=True)

    tour_stop_id = db.Column(db.Integer, db.ForeignKey('tour_stop.id'), nullable=False)

    type = db.Column(db.Enum(ActionType), nullable=False)
    transport_order_number = db.Column(db.String(255), nullable=False)

从试图持久化停止和操作的代码中提取

for stop in load.get('tourStops'):
    tour_stop_to_add = TourStop(...)
    session.add(tour_stop_to_add)
    session.commit()

    for action in stop.get('actions'):
        tour_stop_action_to_add = TourStopAction(tour_stop=tour_stop_to_add.id, type=ActionType(action.get('type')), transport_order_number=action.get('transportOrderNumber'))
        session.add(tour_stop_action_to_add)

创建TourStopAction对象时发生以下错误。

Traceback (most recent call last):
  File "../venv/lib/python3.6/site-packages/flask/app.py", line 2309, in __call__
    return self.wsgi_app(environ, start_response)
  File "../venv/lib/python3.6/site-packages/flask/app.py", line 2295, in wsgi_app
    response = self.handle_exception(e)
  File "../venv/lib/python3.6/site-packages/flask/app.py", line 1741, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "../venv/lib/python3.6/site-packages/flask/_compat.py", line 35, in reraise
    raise value
  File "../venv/lib/python3.6/site-packages/flask/app.py", line 2292, in wsgi_app
    response = self.full_dispatch_request()
  File "../venv/lib/python3.6/site-packages/flask/app.py", line 1815, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "../venv/lib/python3.6/site-packages/flask/app.py", line 1718, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "../venv/lib/python3.6/site-packages/flask/_compat.py", line 35, in reraise
    raise value
  File "../venv/lib/python3.6/site-packages/flask/app.py", line 1813, in full_dispatch_request
    rv = self.dispatch_request()
  File "../venv/lib/python3.6/site-packages/flask/app.py", line 1799, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "../views/views.py", line 26, in import_loads_api
    import_loads()
  File "../services/historical_data/import_historical_data.py", line 53, in import_loads
    store_loads(loads_json)
  File "../services/historical_data/import_historical_data.py", line 81, in store_loads
    transport_order_number=action.get('transportOrderNumber'))
  File "<string>", line 4, in __init__

  File "../venv/lib/python3.6/site-packages/sqlalchemy/orm/state.py", line 424, in _initialize_instance
    manager.dispatch.init_failure(self, args, kwargs)
  File "../venv/lib/python3.6/site-packages/sqlalchemy/util/langhelpers.py", line 66, in __exit__
    compat.reraise(exc_type, exc_value, exc_tb)
  File "../venv/lib/python3.6/site-packages/sqlalchemy/util/compat.py", line 249, in reraise
    raise value
  File "../venv/lib/python3.6/site-packages/sqlalchemy/orm/state.py", line 421, in _initialize_instance
    return manager.original_init(*mixed[1:], **kwargs)
  File "../venv/lib/python3.6/site-packages/sqlalchemy/ext/declarative/base.py", line 748, in _declarative_constructor
    setattr(self, k, kwargs[k])
  File "../venv/lib/python3.6/site-packages/sqlalchemy/orm/attributes.py", line 229, in __set__
    instance_dict(instance), value, None)
  File "../venv/lib/python3.6/site-packages/sqlalchemy/orm/attributes.py", line 815, in set
    value = self.fire_replace_event(state, dict_, value, old, initiator)
  File "../venv/lib/python3.6/site-packages/sqlalchemy/orm/attributes.py", line 835, in fire_replace_event
    state, value, previous, initiator or self._replace_token)
  File "../venv/lib/python3.6/site-packages/sqlalchemy/orm/attributes.py", line 1183, in emit_backref_from_scalar_set_event
    child_state, child_dict = instance_state(child),\
AttributeError: 'int' object has no attribute '_sa_instance_state'
  

AttributeError:“ int”对象没有属性“ _sa_instance_state”

请尝试提供有关如何成功创建并持久保存tour_stop_to_add的帮助。

0 个答案:

没有答案