1

I'm trying to use the oauth2_provider library which provides a model for AccessToken, which foreign keys into a User model. My User model will actually live in a different database from the OAuth2 token models. I can use a router to direct which DB to use for a particular model, but am I correct in concluding that Django would not support a foreign key to a model in a different DB?

If I still wanted to extend from the AbstractAccessToken with my User in a different DB, is there any way that Django allows me to populate the user_id foreign key column at all? Or would I simply need to leave it null and define and have my custom AccessToken class define its own unconstrained external_user_id column?

Nathan
  • 1,396
  • 3
  • 18
  • 32
  • do you know database that support a foreign key to a table in a different DB? – Brown Bear Oct 03 '18 at 06:07
  • Right, I'm not expecting a foreign key constraint to work, but the model comes with an expectation of a foreign key. I guess the question boils down to - is setting the FK to null and defining a new unconstrained id field the recommended way to re-use the model? – Nathan Oct 03 '18 at 06:33

1 Answers1

0

Django doesn't support any ForeignKey operations that span multiple databases. So, as you suggested, I think the best you can do is to provide your own IntegerField for the user and use it manually. Unfortunately that may require a lot of fiddling with that third-party library if it has a lot of internal code that's expecting to pull the user from the database.

Kevin Christopher Henry
  • 46,175
  • 7
  • 116
  • 102