我有一个烧瓶应用程序工厂应用程序,其中包含大量模型和视图。一切都工作正常,直到我将某些模块更改为
backref
以跟上时代的步伐。突然,应用程序无法工作,因为我在不同的模块中收到 sqlalchemy 属性错误,甚至在用户模块中,而这些模块根本没有被触及。 (见下文。)
back_populates
关于 SO 的类似问题的大多数答案,例如:
this
SQLAlchemy: AttributeError: 'Table' object has no attribute 'id' and this
"'Table '对象没有属性'id'”在一个表上具有两个外键的 SQLAlchemy 关系上 重点关注导致问题的特定关系的定义。我不明白它如何适用于在更改为不同模型之前运行良好的模型。切实可行的建议将不胜感激。
focus on definitions of specific relationships causing the issue. I fail to see how it would apply incase of a model that worked fine before changes to a different model. Actionable advice will be greatly appreciated.
@login_manager.user_loader
def load_user(id):
return User.query.get(id)
class User(db.Model):
__tablename__ = 'users'
id = Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
name = Column(String(100), unique=True, index=True, nullable=False)
...
File "C:\Users\user\folder\venv\Lib\site-packages\flask\app.py", line 1498, in __call__
return self.wsgi_app(environ, start_response)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\user\folder\venv\Lib\site-packages\flask\app.py", line 1476, in wsgi_app
response = self.handle_exception(e)
^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\user\folder\venv\Lib\site-packages\flask\app.py", line 1473, in wsgi_app
response = self.full_dispatch_request()
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\user\folder\venv\Lib\site-packages\flask\app.py", line 882, in full_dispatch_request
rv = self.handle_user_exception(e)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\user\folder\venv\Lib\site-packages\flask\app.py", line 880, in full_dispatch_request
rv = self.dispatch_request()
^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\user\folder\venv\Lib\site-packages\flask\app.py", line 865, in dispatch_request
return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args) # type: ignore[no-any-return]
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "c:\users\user\folder\app\templates\main\views.py", line 33, in home
return render_template('/layout/project/home.html', **context, **common_context)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\user\folder\venv\Lib\site-packages\flask\templating.py", line 150, in render_template
return _render(app, template, context)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\user\folder\venv\Lib\site-packages\flask\templating.py", line 127, in _render
app.update_template_context(context)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\user\folder\venv\Lib\site-packages\flask\app.py", line 493, in update_template_context
context.update(self.ensure_sync(func)())
^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\user\folder\venv\Lib\site-packages\flask_login\utils.py", line 405, in _user_context_processor
return dict(current_user=_get_user())
^^^^^^^^^^^
File "C:\Users\user\folder\venv\Lib\site-packages\flask_login\utils.py", line 370, in _get_user
current_app.login_manager._load_user()
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\user\folder\venv\Lib\site-packages\flask_login\login_manager.py", line 376, in _load_user
user = self._load_user_from_remember_cookie(cookie)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\user\folder\venv\Lib\site-packages\flask_login\login_manager.py", line 420, in _load_user_from_remember_cookie
user = self._user_callback(user_id)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "c:\users\user\folder\app\models\user.py", line 24, in load_user
return User.query.get(id)
^^^^^^^^^^
File "C:\Users\user\folder\venv\Lib\site-packages\flask_sqlalchemy\model.py", line 30, in __get__
return cls.query_class(
File "C:\Users\user\folder\venv\Lib\site-packages\sqlalchemy\orm\query.py", line 273, in __init__
self._set_entities(entities)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\user\folder\venv\Lib\site-packages\sqlalchemy\orm\query.py", line 282, in _set_entities
self._raw_columns = [
File "C:\Users\user\folder\venv\Lib\site-packages\sqlalchemy\orm\query.py", line 283, in <listcomp>
coercions.expect(
^
File "C:\Users\user\folder\venv\Lib\site-packages\sqlalchemy\sql\coercions.py", line 406, in expect
insp._post_inspect
^^^^^^^^^^^^^^^^^^
File "C:\Users\user\folder\venv\Lib\site-packages\sqlalchemy\util\langhelpers.py", line 1254, in __get__
obj.__dict__[self.__name__] = result = self.fget(obj)
^^^^^^^^^^^^^^
File "C:\Users\user\folder\venv\Lib\site-packages\sqlalchemy\orm\mapper.py", line 2693, in _post_inspect
self._check_configure()
^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\user\folder\venv\Lib\site-packages\sqlalchemy\orm\mapper.py", line 2374, in _check_configure
_configure_registries({self.registry}, cascade=True)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\user\folder\venv\Lib\site-packages\sqlalchemy\orm\mapper.py", line 4186, in _configure_registries
_do_configure_registries(registries, cascade)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\user\folder\venv\Lib\site-packages\sqlalchemy\orm\mapper.py", line 4228, in _do_configure_registries
mapper._post_configure_properties()
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\user\folder\venv\Lib\site-packages\sqlalchemy\orm\mapper.py", line 2391, in _post_configure_properties
prop.init()
^^^^^^^^^^^
File "C:\Users\user\folder\venv\Lib\site-packages\sqlalchemy\orm\interfaces.py", line 544, in init
self.do_init()
^^^^^^^^^^^^^^
File "C:\Users\user\folder\venv\Lib\site-packages\sqlalchemy\orm\relationships.py", line 1631, in do_init
self._process_dependent_arguments()
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\user\folder\venv\Lib\site-packages\sqlalchemy\orm\relationships.py", line 1673, in _process_dependent_arguments
rel_arg._resolve_against_registry(self._clsregistry_resolvers[1])
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\user\folder\venv\Lib\site-packages\sqlalchemy\orm\relationships.py", line 261, in _resolve_against_registry
self.resolved = clsregistry_resolver(
File "C:\Users\user\folder\venv\Lib\site-packages\sqlalchemy\orm\clsregistry.py", line 532, in __call__
x = eval(self.arg, globals(), self._dict)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "<string>", line 1, in <module>
AttributeError: 'Table' object has no attribute 'id'
这个错误“AttributeError: 'Table' object has no attribute 'id'”通常意味着你在 SQLAlchemy 关系中引用了一个列,该列应该引用表本身,反之亦然。不幸的是,没有提供更改后的模型代码,因此我无法指出确切的原因。但是,我可以提供一个逐步的调试过程,并提供一些示例来帮助你识别和解决问题。
调试步骤
- 定位确切的失败关系:
-
错误消息提供了一个 traceback。仔细查看 traceback 以找到引发错误的确切行。这将指向涉及有问题的
relationship
定义的模型和文件。 -
检查关系定义:
-
确定导致错误的
relationship
定义后,请仔细检查以下内容:-
backref
或back_populates
参数: 确保这些参数引用的列名在相关模型中作为实际列存在,并且它们是关系中的正确目标列。 -
primaryjoin
参数(如果使用): 验证连接条件是否正确,并使用相关模型中的实际列(而不是试图引用表本身的id
属性)。
-
常见陷阱和解决方案
以下是使用
backref
和
back_populates
时导致此错误的一些常见错误:
1. 在
backref
或
back_populates
中引用表而不是列:
```python class Parent(db.Model): tablename = 'parents' id = Column(Integer, primary_key=True) children = relationship("Child", backref="Parent") # 错误:引用了表名,而不是列名
class Child(db.Model): tablename = 'children' id = Column(Integer, primary_key=True) parent_id = Column(Integer, ForeignKey('parents.id')) ```
解决方案:
在
backref
中使用列名:
python
children = relationship("Child", backref="parent") # 正确:引用 'parent_id' 列隐式创建的 'parent' 属性
2. 在关系两侧使用
backref
和
back_populates
:
```python class Parent(db.Model): # ... children = relationship("Child", backref="parent")
class Child(db.Model): # ... parent = relationship("Parent", back_populates="children") # 错误:同时使用了 backref 和 back_populates ```
解决方案:
选择
backref
或
back_populates
,而不是同时使用:
```python # 选项 1:仅使用 backref class Parent(db.Model): # ... children = relationship("Child", backref="parent")
class Child(db.Model): # ...
# 选项 2:仅使用 back_populates class Parent(db.Model): # ... children = relationship("Child", back_populates="parent")
class Child(db.Model): # ... parent = relationship("Parent") ```
3. 在连接条件中引用错误的列:
```python class Parent(db.Model): # ...
class Child(db.Model): # ... parent = relationship("Parent", primaryjoin="Parent.id == Child.parent_id") # 错误:在连接条件中引用了 'Parent' ```
解决方案: 使用表名或别名来引用列:
python
parent = relationship("Parent", primaryjoin="Parent.id == Child.parent_id") # 正确:使用表名引用列
仔细检查你的关系定义,并根据这些常见错误进行更正。如果问题仍然存在,请提供你的模型代码,以便我提供更具体的帮助!
标签:python,flask,sqlalchemy,flask-sqlalchemy,attributeerror From: 78785903