python - 使用 peewee 的数据库之间的外键

标签 python mysql peewee

我有两个遗留的 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/

相关文章:

python - 如何在 Flask Peewee 中使用更新查询?

python - 使用 Peewee 追溯创建索引?

python - protobuf3,python - 如何将元素设置为 map<string,OtherMessage> 字典?

mysql - SQL 被查询卡住

python - 检查字符串是否包含python中的某些字符

php - 如何联合所有+1表?

php - 调用未定义函数 mysql_connect() 错误,即使安装了 PHP

python - 如何在 Peewee ORM 中使用 fn 对象

python - Argparse 默认为函数始终被调用

python - 等效于 Python 中的字段的 NotImplementedError