V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
V2EX  ›  CRITICAL7  ›  全部回复第 1 页 / 共 1 页
回复总数  1
2020-06-03 01:31:19 +08:00
回复了 LennonChin 创建的主题 Python 请问, SQLAlchemy 无外键约束的多对多关系模型该怎么写啊?
emm 就是这么写吧大概。。官网 tutorial 改了改

```
class User(Base):
id = Column(Integer, primary_key=True)


class UserAddress(Base):
id = Column(Integer, primary_key=True)
user_id = Column(Integer)
address_id = Column(Integer)

user = relationship("User", primaryjoin="foreign(UserAddress.user_id) == User.id")
address = relationship("Address", primaryjoin="foreign(UserAddress.address_id) == Address.id")


class Address(Base):
id = Column(Integer, primary_key=True)
```
关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   4934 人在线   最高记录 6679   ·     Select Language
创意工作者们的社区
World is powered by solitude
VERSION: 3.9.8.5 · 16ms · UTC 09:34 · PVG 17:34 · LAX 02:34 · JFK 05:34
Developed with CodeLauncher
♥ Do have faith in what you're doing.