gpt4 book ai didi

python - SQLAlchemy: AttributeError: 'Table' 对象没有属性 'id'

转载 作者:太空宇宙 更新时间:2023-11-03 20:24:57 25 4
gpt4 key购买 nike

我正在构建一个数据库,将我的本地产品目录与外国产品目录相匹配。当我有唯一的表'Catalog'时,SQLAlchemy 工作得很好。添加表 CompetitorsCompetitors_catalog 后,我向两个新表添加记录的所有尝试均返回错误。

AttributeError: 'Table' object has no attribute 'id'

我多次更改/删除/播放CompetitorsCompetitors_catalog之间的关系,通过flask-migrate来回删除和创建表,但不断收到相同的错误。像 Competitors.query.all() 这样的简单查询返回相同的结果。删除了 db.relationship() - 相同的结果。然后我最终回滚到一个干净的数据库,并从头开始使用 Flask-migrate 启动它。之后,我开始收到相同的错误,尝试将记录添加到Catalog表,在我回滚之前,它运行良好。这很奇怪,因为我没有对那部分代码进行任何更改。

class Catalog(db.Model):
__tablename__ = 'catalog'

id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(60), index=True, nullable=False)
parent_id = db.Column(db.Integer, db.ForeignKey('catalog.id'), default=None)
url = db.Column(db.String())
created = db.Column(db.DateTime, default=datetime.now())
last_modified = db.Column(db.DateTime, index=True, onupdate=datetime.now())

categories = db.relationship('Catalog', remote_side='catalog.id', backref='parent')

class Competitors(db.Model):
__tablename__ = 'competitors'

id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(40), index=True, nullable=False)
base_url = db.Column(db.String())
cat_url = db.Column(db.String())
created = db.Column(db.DateTime, default=datetime.now())
last_modified = db.Column(db.DateTime, onupdate=datetime.now(), default=datetime.now())

sells = db.relationship('Competitors_catalog', backref='competitor', cascade="all, delete-orphan", passive_deletes=True, lazy='dynamic')

class CompCatalog(db.Model):
__tablename__ = 'compcatalog'

id = db.Column(db.Integer, primary_key=True)
out_id = db.Column(db.Integer)
name = db.Column(db.String(60), index=True, nullable=False)
top_section = db.Column(db.Integer, default=None)
comp_id = db.Column(db.Integer, db.ForeignKey('competitors.id', ondelete="CASCADE"))
url = db.Column(db.String())
created = db.Column(db.DateTime, default=datetime.now())
last_modified = db.Column(db.DateTime, index=True, onupdate=datetime.now())

简单创建新实例会返回错误。但在回滚之前工作正常。

from .models import Catalog

name = 'Apple'
rec = Catalog(name=name)
Traceback (most recent call last):
File "<console>", line 1, in <module>
File ".../app/helpers.py", line 31, in create_catalog
rec = Catalog(name=name)
File "<string>", line 2, in __init__
File ".../.virtualenvs/.../lib/python3.6/site-packages/sqlalchemy/orm/instrumentation.py", line 376, in _new_state_if_none
state = self._state_constructor(instance, self)
File ".../.virtualenvs/.../lib/python3.6/site-packages/sqlalchemy/util/langhelpers.py", line 855, in __get__
obj.__dict__[self.__name__] = result = self.fget(obj)
File ".../.virtualenvs/.../lib/python3.6/site-packages/sqlalchemy/orm/instrumentation.py", line 202, in _state_constructor
self.dispatch.first_init(self, self.class_)
File ".../.virtualenvs/.../lib/python3.6/site-packages/sqlalchemy/event/attr.py", line 322, in __call__
fn(*args, **kw)
File ".../.virtualenvs/.../lib/python3.6/site-packages/sqlalchemy/orm/mapper.py", line 3360, in _event_on_first_init
configure_mappers()
File ".../.virtualenvs/.../lib/python3.6/site-packages/sqlalchemy/orm/mapper.py", line 3248, in configure_mappers
mapper._post_configure_properties()
File ".../.virtualenvs/.../lib/python3.6/site-packages/sqlalchemy/orm/mapper.py", line 1947, in _post_configure_properties
prop.init()
File ".../.virtualenvs/.../lib/python3.6/site-packages/sqlalchemy/orm/interfaces.py", line 196, in init
self.do_init()
File ".../.virtualenvs/.../lib/python3.6/site-packages/sqlalchemy/orm/relationships.py", line 1860, in do_init
self._process_dependent_arguments()
File ".../.virtualenvs/.../lib/python3.6/site-packages/sqlalchemy/orm/relationships.py", line 1889, in _process_dependent_arguments
setattr(self, attr, attr_value())
File ".../.virtualenvs/.../lib/python3.6/site-packages/sqlalchemy/ext/declarative/clsregistry.py", line 294, in __call__
x = eval(self.arg, globals(), self._dict)
File "<string>", line 1, in <module>
AttributeError: 'Table' object has no attribute 'id'

最佳答案

remote_side 参数可以作为可计算字符串传递,您已完成此操作:

remote_side='catalog.id'

问题在于,它所评估的命名空间包含 Table 对象和模型类,而您引用的是 Table 而不是模型类。只需将远程端更改为

remote_side='Catalog.id'

或者直接在类主体中传递id列:

class Catalog(db.Model):
__tablename__ = 'catalog'

id = db.Column(db.Integer, primary_key=True)
...
categories = db.relationship('Catalog', remote_side=id, backref='parent')

关于python - SQLAlchemy: AttributeError: 'Table' 对象没有属性 'id',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57904984/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com