bernardopires / django-tenant-schemas

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

Migrations not run on save() if model supplies its own PK #533

Open pvandegeer opened 6 years ago

pvandegeer commented 6 years ago

If a Tenant model supplies its own PK, for example a UUID, then create_schema() is never called.

The code for TenantMixin check for the availability of the pk to determine if a model is new: https://github.com/bernardopires/django-tenant-schemas/blob/master/tenant_schemas/models.py#L57 This doesn't work if the key is self supplied.

Possible fix: Django >=1.8 supports self._state.adding which returns True if the instance isn't saved yet. See also: https://stackoverflow.com/questions/2037320/what-is-the-canonical-way-to-find-out-if-a-django-model-is-saved-to-db/19379636#19379636