Hello Djangonauts,
I am experiencing an issue with migrating some database changes in my Django project. I am specifically trying to create some related tables that are linked via TenantForeignKey
. The package I am using in django_multitenant from Citus. Please see a snippet of my models below:
class Account(TenantModel):
name = models.CharField(max_length=255)
code = models.CharField(max_length=3, unique=True)
domain = models.CharField(max_length=255, unique=True)
email = models.EmailField(max_length=255)
website_url = models.CharField(max_length=255)
terms_of_use_url = models.CharField(max_length=255)
privacy_policy_url = models.CharField(max_length=255)
uuid = models.UUIDField(default=uuid.uuid4, editable=False)
created_at = models.DateTimeField(default=timezone.now)
updated_at = models.DateTimeField(auto_now=True)
class TenantMeta:
tenant_field_name = "id"
class Meta:
db_table = "accounts"
class Branch(TenantModel):
uuid = models.UUIDField(default=uuid.uuid4, editable=False)
name = models.CharField(max_length=255)
description = models.CharField(max_length=255, null=True, blank=True)
primary_phone = PhoneNumberField()
secondary_phone = PhoneNumberField(null=True, blank=True)
address = models.CharField(max_length=255)
city = models.CharField(max_length=255)
parish = models.CharField(max_length=255)
code = models.CharField(max_length=12, unique=True)
enabled = models.BooleanField(default=True)
account = models.ForeignKey(to=Account, on_delete=models.CASCADE, related_name="branches")
created_at = models.DateTimeField(default=timezone.now)
updated_at = models.DateTimeField(auto_now=True)
class TenantMeta:
tenant_field_name = "account_id"
class Meta:
db_table = "account_branches"
class BranchWorkingHours(TenantModel):
class Day(models.TextChoices):
MONDAY = "monday", "Monday"
TUESDAY = "tuesday", "Tuesday"
WEDNESDAY = "wednesday", "Wednesday"
THURSDAY = "thursday", "Thursday"
FRIDAY = "friday", "Friday"
SATURDAY = "saturday", "Saturday"
SUNDAY = "sunday", "Sunday"
uuid = models.UUIDField(default=uuid.uuid4, editable=False)
branch = TenantForeignKey(Branch, on_delete=models.CASCADE)
day = models.CharField(max_length=10, choices=Day.choices)
opening_time = models.TimeField()
closing_time = models.TimeField()
account = models.ForeignKey(to=Account, on_delete=models.CASCADE)
created_at = models.DateTimeField(default=timezone.now)
updated_at = models.DateTimeField(auto_now=True)
class TenantMeta:
tenant_field_name = "account_id"
class Meta:
db_table = "account_branch_working_hours"
constraints = [
models.UniqueConstraint(fields=["branch", "day"], name="unique_branch_day"),
]
My I run python manage.py makemigrations tenant
my migration files are generated without issue. However, when I try to perform the actual migration by running python manage.py migrate
I am greeted with the following error:
Running migrations:
Applying tenant.0004_branchworkinghours_and_more...Traceback (most recent call last):
File "/home/dalean/PycharmProjects/courierconnect/venv/lib/python3.10/site-packages/django/db/backends/utils.py", line 89, in _execute
return self.cursor.execute(sql, params)
psycopg2.errors.InvalidForeignKey: there is no unique constraint matching given keys for referenced table "account_branches"
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "/home/dalean/PycharmProjects/courierconnect/manage.py", line 21, in <module>
main()
File "/home/dalean/PycharmProjects/courierconnect/manage.py", line 17, in main
execute_from_command_line(sys.argv)
File "/home/dalean/PycharmProjects/courierconnect/venv/lib/python3.10/site-packages/django/core/management/__init__.py", line 446, in execute_from_command_line
utility.execute()
File "/home/dalean/PycharmProjects/courierconnect/venv/lib/python3.10/site-packages/django/core/management/__init__.py", line 440, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/dalean/PycharmProjects/courierconnect/venv/lib/python3.10/site-packages/django/core/management/base.py", line 402, in run_from_argv
self.execute(*args, **cmd_options)
File "/home/dalean/PycharmProjects/courierconnect/venv/lib/python3.10/site-packages/django/core/management/base.py", line 448, in execute
output = self.handle(*args, **options)
File "/home/dalean/PycharmProjects/courierconnect/venv/lib/python3.10/site-packages/django/core/management/base.py", line 96, in wrapped
res = handle_func(*args, **kwargs)
File "/home/dalean/PycharmProjects/courierconnect/venv/lib/python3.10/site-packages/django/core/management/commands/migrate.py", line 349, in handle
post_migrate_state = executor.migrate(
File "/home/dalean/PycharmProjects/courierconnect/venv/lib/python3.10/site-packages/django/db/migrations/executor.py", line 135, in migrate
state = self._migrate_all_forwards(
File "/home/dalean/PycharmProjects/courierconnect/venv/lib/python3.10/site-packages/django/db/migrations/executor.py", line 167, in _migrate_all_forwards
state = self.apply_migration(
File "/home/dalean/PycharmProjects/courierconnect/venv/lib/python3.10/site-packages/django/db/migrations/executor.py", line 249, in apply_migration
with self.connection.schema_editor(
File "/home/dalean/PycharmProjects/courierconnect/venv/lib/python3.10/site-packages/django/db/backends/base/schema.py", line 164, in __exit__
self.execute(sql)
File "/home/dalean/PycharmProjects/courierconnect/venv/lib/python3.10/site-packages/django_multitenant/backends/postgresql/base.py", line 119, in execute
super().execute(statement)
File "/home/dalean/PycharmProjects/courierconnect/venv/lib/python3.10/site-packages/django/db/backends/base/schema.py", line 199, in execute
cursor.execute(sql, params)
File "/home/dalean/PycharmProjects/courierconnect/venv/lib/python3.10/site-packages/django/db/backends/utils.py", line 102, in execute
return super().execute(sql, params)
File "/home/dalean/PycharmProjects/courierconnect/venv/lib/python3.10/site-packages/django/db/backends/utils.py", line 67, in execute
return self._execute_with_wrappers(
File "/home/dalean/PycharmProjects/courierconnect/venv/lib/python3.10/site-packages/django/db/backends/utils.py", line 80, in _execute_with_wrappers
return executor(sql, params, many, context)
File "/home/dalean/PycharmProjects/courierconnect/venv/lib/python3.10/site-packages/django/db/backends/utils.py", line 84, in _execute
with self.db.wrap_database_errors:
File "/home/dalean/PycharmProjects/courierconnect/venv/lib/python3.10/site-packages/django/db/utils.py", line 91, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/home/dalean/PycharmProjects/courierconnect/venv/lib/python3.10/site-packages/django/db/backends/utils.py", line 89, in _execute
return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: there is no unique constraint matching given keys for referenced table "account_branches"
I really hope someone here can point me in the right direction. Thanks in advance for your time.