Search code examples
pythonmysqlpeewee

foreign keys between databases with peewee


I have two legacy MySQL databases for which I'd like to define an ORM class-model in peewee (python). Specifically, one database holds front-end data, the other back-end data and some information between the tables of the databases are linked with foreign keys from one database to the other.

Example code (not the actual code, inspired by the example in quick start):

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

Going through the docs, I couldn't find a direct way to link the foreign keys between tables. Also, I've tried generating a peewee model using the supplied pwiz.py script, which worked successfully on the front-end database, but not on the back-end (probably because the back-end only seems to refer to the front-end and not vice-versa). Nevertheless, I'd like to ask whether such a model with two databases is possible.


Solution

  • Peewee does not support foreign keys across multiple databases.