citusdata / django-multitenant

Python/Django support for distributed multi-tenant databases like Postgres+Citus
MIT License
721 stars 119 forks source link

Cannot plan queries which include both local and distributed relations #18

Closed juliocesar-io closed 6 years ago

juliocesar-io commented 6 years ago

When I try to do the migrations as the docs example, I got this error:

django.db.utils.InternalError: cannot plan queries which include both local and distributed relations
CONTEXT:  SQL statement "SELECT fk."account_id" FROM ONLY "public"."others_other" fk LEFT OUTER JOIN ONLY "public"."core_account" pk ON ( pk."id" OPERATOR(pg_catalog.=) fk."account_id") WHERE pk."id" IS NULL AND (fk."account_id" IS NOT NULL)"

Is this problem related with the models being separated on different apps?

My models are:

core/models.py

class Account(models.Model):

   plan = models.CharField(max_length=25)

others/models.py

class Other(models.Model):

    account = models.ForeignKey(Account, on_delete=models.CASCADE)

    class Meta(object):
        unique_together = ["id", "account"]

My Migrations:

core/migrations/0002_distribute_tables.py

    ...
    operations = [
        migrations.RunSQL(
            "SELECT create_distributed_table('core_account','id')"
        ),
    ]

others/migrations/0002_remove_simple_pk.py

    ...

    operations = [
        migrations.RunSQL(
            "ALTER TABLE others_other DROP CONSTRAINT others_other _pkey;",
                "ALTER TABLE others_other ADD CONSTRAINT others_other _pkey PRIMARY KEY (account_id, id)"
        ),
    ]

others/migrations/0003_distribute_tables.py

    ...

    operations = [
        migrations.RunSQL(
            "SELECT create_distributed_table('others_other','account_id')"
        ),
    ]
juliocesar-io commented 6 years ago

I'm using Django 2.0 with Python 3.6 and Citus's official Docker image.

saicitus commented 6 years ago

@juliocesar-io Can you first distribute both the tables and then try adding the foreign keys? That should fix this issue.

saicitus commented 6 years ago

Closing this issue because you'd need make both the tables distributed/referenced to do successful joins.

juliocesar-io commented 6 years ago

It worked well. 👍