我有两个遗留的 MySQL 数据库,我想在 peewee (python) 中为其定义一个 ORM 类模型。具体来说,一个数据库保存前端数据,另一个后端数据和数据库表之间的一些信息通过一个数据库到另一个数据库的外键链接.
示例代码(不是实际代码,灵感来自快速入门中的示例):
import peewee
frontend = peewee.MySQLDatabase('frontend', host=host, user=user, passwd=passwd)
backend = peewee.MySQLDatabase('backend', host=host, user=user, passwd=passwd)
class User(peewee.Model):
name = peewee.CharField()
class Meta:
database = frontend
class Tweet(peewee.Model):
user = peewee.ForeignKeyField(User, related_name='tweets')
content = peewee.TextField()
class Meta:
database = backend
遍历 docs ,我找不到直接链接表之间外键的方法。此外,我尝试使用提供的 pwiz.py script 生成一个 peewee 模型,它在前端数据库上成功运行,但在后端却没有(可能是因为后端似乎只指前端,反之亦然)。不过,我想问一下这样一个有两个数据库的模型是否可行。
最佳答案
Peewee 不支持跨多个数据库的外键。
关于python - 使用 peewee 的数据库之间的外键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17235882/