【发布时间】:2015-09-04 11:23:48
【问题描述】:
我的一个模型有以下关系:
class User(Base):
account = relationship("Account")
我想手动设置帐号。
我的第一次尝试是这样的:
class User(Base):
account = relationship("Account")
accounts_id = Column(Integer, ForeignKey("accounts.id"), nullable=True)
@classmethod
def from_json(cls, json):
appointment = Appointment()
appointment.account_id = json["account_id"]
return appointment
上述方法不起作用。我们不能参考这个专栏,因为 SQLAlchemy 不合适。这是一个例外:
sqlalchemy.exc.InvalidRequestError: Implicitly combining column users.accounts_id with column users.accounts_id under attribute 'accounts_id'. Please configure one or more attributes for these same-named columns explicitly.
我尝试通过文档搜索并尝试通过多种方式获取属性,但我无法找到,更不用说设置它了。
print(self.account.account_id)
print(self.account.relationhip)
print(self.account.properties)
print(self.account.primaryjoin)
有什么想法吗?
[编辑-在上面添加异常]
【问题讨论】:
-
你的类方法有点不连贯。
self应该来自哪里?您是否省略了self = cls()? -
@SingleNegationElimination - 修复它以反映我实际拥有的东西
-
该声明会发生什么,您预计会发生什么?
-
你怎么打电话给
User.from_json()?您有任何要处理的数据吗? -
@SingleNegationElimination - 是的,我传递给它一个 JSON 字典。我在上面添加了一个例外以更准确地反映问题。我的自定义构造函数运行良好,但由于来自 ORM 的
InvalidRequestError无法设置属性
标签: python exception orm sqlalchemy foreign-keys