Django admin migration with postgres_fdw

I use postgres_fdw to create relations between my databases Here I have a foreign model with the name:

class User(AbstractUser):
    ...
    class Meta:
        db_table = '"foreign_user"."accounts__user"'
        managed = False

which pointing to this model in another django project:

class User(AbstractUser):
    ...
    class Meta:
        db_table = 'accounts__user'
        managed = False

I did run the python manage.py makemigrations and python manage.py migrate I got an error from django admin

Applying admin.0001_initial...Traceback (most recent call last): ... django.db.utils.ProgrammingError: referenced relation "accounts__user" is not a table

but when I ran migrations with specific apps (my installed app only) and the admin migrations didn't run, everything worked well and all relations with User model created.

I also made sure that I set AUTH_USER_MODEL='accounts.User I already made the foreing servers and foreign schemas as needed I did check the database, and all of the tables and relations were fine

I found out
the main reason in how other packages (including django admin app) use ForeignKey to User model

class LogEntry(models.Model):
    ...
    user = models.ForeignKey(
        settings.AUTH_USER_MODEL,
        models.CASCADE,
        verbose_name=_("user"),
    )

if you want to have ForeignKey between a local model and a foreign model, you have to tell django to do not enforce db constraint

models.ForeignKey(..., db_constraint=False)

so since we cannot change the third party models or migrations, the best approach is using a dummy model for AUTH_USER_MODEL and then route the query to the foreign model using a custom manager

Вернуться на верх