bernardopires / django-tenant-schemas

Tenant support for Django using PostgreSQL schemas.
https://django-tenant-schemas.readthedocs.org/en/latest/
MIT License
1.45k stars 425 forks source link

[Solution] Unable to create the django_migrations -relation already exists #695

Open benshaji-sequoiaat opened 11 months ago

benshaji-sequoiaat commented 11 months ago

I'd like to publish a solution to a problem which i'm sure some would face in future. If you are familiar, the error is not very obvious and it explains something implicit.

When you run ./manage.py migrate , you would see a long traceback along with this at the end. migrations.exceptions.MigrationSchemaMissing: Unable to create the django_migrations table (relation "django_migrations" already exists. This in Django world means issues with db inconsistencies and likely hard to get back. I had faced this issue myself couple of time.

The issue is in your public schema where you store your tenant info. In my case, its tenant_tenant and tenant_domain in public. If you look close there could be some schemas which are created but not showing up in the actual database schema list. This could be because the schema was not created successfully or some other issues.

Just remove the entries which you donot see in the list of db schema from tenant_domain and then the refs from tenant_tenant. I will not encourage numbers into schema names as well. Then do run the migration again

Feel free to ask in the comments incase of any questions :)

classwood commented 5 months ago

This insight helped my project and saved my data!! Thank you so much!!