简体   繁体   English

蟒蛇。 SQL Alchemy NotImplementedError:此表达式不支持运算符“ getitem”

[英]Python. SQL Alchemy NotImplementedError: Operator 'getitem' is not supported on this expression

I create record in DB through sql alchemy. 我通过sql alchemy在DB中创建记录。 The next step is trying to capture the id of the created object into another table that has fk on the created object (assign_resource_to_user) . 下一步是尝试将创建的对象的ID捕获到另一个在创建的对象上具有fk的表中(assign_resource_to_user)。 This is my code: 这是我的代码:

from dev_tools.models.user import User
from dev_tools.models.resource Resource

resource = Resource(
    code=self.message_body['code'],
    description=self.message_body['description'],
    crew_id=None,
    catalog_resource_type_id=self.message_body['catalog_resource_type_id'],
    catalog_resource_status_id=self.message_body['catalog_resource_status_id'],
    created_at=datetime.utcnow(),
    created_by=None,
    is_available=self.message_body['is_available'],
)
self.db_session.add(resource)
self.db_session.flush()

assign_resource_to_user = self.db_session.query(
    User
).filter(
    User.id == self.user_info.id
).update(
    User.resource_id == resource.id
)
self.db_session.add(assign_resource_to_user)

I have error: 我有错误:

Traceback (most recent call last):
  File "/home/Документы/project/ResourseManagment/rm-private-application-server/apps/controller/helpers/data_processing/action/custom/resource_from_a_user.py", line 227, in <module>
    resources.create_record_in_db()
  File "/home/Документы/project/ResourseManagment/rm-private-application-server/apps/controller/helpers/data_processing/action/custom/resource_from_a_user.py", line 211, in create_record_in_db
    User.resource_id == resource.id
  File "/home/Документы/project/venv/lib/python3.7/site-packages/SQLAlchemy-1.2.14-py3.7-linux-x86_64.egg/sqlalchemy/orm/query.py", line 3486, in update
  File "/home/Документы/project/venv/lib/python3.7/site-packages/SQLAlchemy-1.2.14-py3.7-linux-x86_64.egg/sqlalchemy/orm/persistence.py", line 1334, in exec_
  File "/home/Документы/project/venv/lib/python3.7/site-packages/SQLAlchemy-1.2.14-py3.7-linux-x86_64.egg/sqlalchemy/orm/persistence.py", line 1405, in _do_pre_synchronize
  File "/home/Документы/project/venv/lib/python3.7/site-packages/SQLAlchemy-1.2.14-py3.7-linux-x86_64.egg/sqlalchemy/orm/persistence.py", line 1540, in _additional_evaluators
  File "/home/Документы/project/venv/lib/python3.7/site-packages/SQLAlchemy-1.2.14-py3.7-linux-x86_64.egg/sqlalchemy/orm/persistence.py", line 1473, in _resolved_values_keys_as_propnames
  File "/home/Документы/project/venv/lib/python3.7/site-packages/SQLAlchemy-1.2.14-py3.7-linux-x86_64.egg/sqlalchemy/orm/persistence.py", line 1457, in _resolved_values
  File "/home/Документы/project/venv/lib/python3.7/site-packages/SQLAlchemy-1.2.14-py3.7-linux-x86_64.egg/sqlalchemy/sql/operators.py", line 411, in __getitem__
  File "/home/Документы/project/venv/lib/python3.7/site-packages/SQLAlchemy-1.2.14-py3.7-linux-x86_64.egg/sqlalchemy/sql/elements.py", line 694, in operate
  File "/home/Документы/project/venv/lib/python3.7/site-packages/SQLAlchemy-1.2.14-py3.7-linux-x86_64.egg/sqlalchemy/sql/operators.py", line 411, in __getitem__
  File "<string>", line 1, in <lambda>
  File "/home/Документы/project/venv/lib/python3.7/site-packages/SQLAlchemy-1.2.14-py3.7-linux-x86_64.egg/sqlalchemy/sql/type_api.py", line 63, in operate
  File "/home/Документы/project/venv/lib/python3.7/site-packages/SQLAlchemy-1.2.14-py3.7-linux-x86_64.egg/sqlalchemy/sql/default_comparator.py", line 192, in _getitem_impl
  File "/home/Документы/project/venv/lib/python3.7/site-packages/SQLAlchemy-1.2.14-py3.7-linux-x86_64.egg/sqlalchemy/sql/default_comparator.py", line 197, in _unsupported_impl
NotImplementedError: Operator 'getitem' is not supported on this expression

Error in this string: 该字符串错误:

).update(
    User.resource_id == resource.id
)

Maybe someone know how fix it or have same problem and can help me solve it. 也许有人知道如何解决或有相同的问题,可以帮助我解决。 Thanks. 谢谢。

Query.update() expects a mapping of attributes to values as the first positional argument, but you've passed it an SQL expression language object. Query.update()希望将属性到值的映射作为第一个位置参数,但是您已将其传递给SQL表达式语言对象。 Note that Query.update() is a bulk operation and does not return a model instance or such, but the number of matched rows. 请注意, Query.update()是批量操作,不返回模型实例等,而是返回匹配的行数。 So instead do: 所以改为:

self.db_session.query(
    User
).filter(
    User.id == self.user_info.id
).update(
    {User.resource_id: resource.id},
    synchronize_session=False  # Change this according to your needs
)
# No add

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM