如何创建具有复合外键的模型?
任何人都知道如何创建一个具有另一个模型的复合外键的模型。
例如
UserInfo:userId、password、key、...
GeoInfo:id、userId、password、storeName、 ...
在上面的示例模型中。想要使用两个字段(用户 ID、密码)将 GeoInfo 链接到 UserInfo。
注意:我知道如何用一个外键链接两个模型。
任何指导表示赞赏。
更新1 我找到了这篇文章-> Django 或类似的复合主键
不幸的是,公认的答案是由于要求(客户规范)在 GeoInfo 中存在两个字段(UserInfo.userId、UserInfo.password),因此不适用于我的情况。
更新2 看来django不支持复合外键。有什么解决方法吗?
anybody knows on how to create a model with compound foreign key to another model.
e.g.
UserInfo: userId, password, key, ...
GeoInfo: id, userId, password, storeName, ...
In the above sample models. Want to link GeoInfo to UserInfo using two fields (userId, password).
note:I know how to link two models with one foreign key.
Any guidance is appreciated.
UPDATES1
I found this post -> Django or similar for composite primary keys
Unfortunately, the accepted answer there is not applicable in my case due to requirement (client's specifications) to have exist the two fields(UserInfo.userId, UserInfo.password) in GeoInfo.
UPDATES2
so it seems, django doesn't support compound foreign keys. any workaround guys?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是什么阻止您使用两个外键字段引用模型 A?
例如,
确保使用“相关名称”来防止冲突。如果这不能回答您的问题,提供您正在尝试做的事情的示例会有所帮助......
What's keeping you from having two ForeignKey field referencing Model A?
e.g.
make sure to use 'related_name' to prevent conflicts. If this doesn't answer your question, providing an example of what you're trying to do would help...