I want to write an abstract model mixin, that I can use to make OneToOne - relations to the user model. Here is my code:
from django.conf import settings
from django.db import models
class Userable(models.Model):
user = models.OneToOneField(
settings.AUTH_USER_MODEL,
on_delete=models.CASCADE
)
class Meta:
abstract = True
I've written the following test for this model:
class TestUserable(TestCase):
mixin = Userable
def setUp(self):
user = User.objects.create_user(
email="[email protected]",
name="Test User",
password="test1234test"
)
self.user = user
self.model = ModelBase(
'__TestModel__' + self.mixin.__name__, (self.mixin,),
{'__module__': self.mixin.__module__}
)
with connection.schema_editor() as schema_editor:
schema_editor.create_model(self.model)
def test_user(self):
self.model.objects.create(user=self.user)
self.assertEqual(self.model.objects.count(), 1)
def tearDown(self):
with connection.schema_editor() as schema_editor:
schema_editor.delete_model(self.model)
My problem is, that this test in it's tearDown()
method throws the follwing error:
django.db.utils.OperationalError: cannot DROP TABLE "core___testmodel__userable" because it has pending trigger events
What could be the cause of this? I did run python manage.py makemigrations
and python manage.py migrate
, but there are no pending migrations (as is expected, since this is an abstract model).
EDIT: It seems to have something to do with OneToOneFields or ForeignKeys (relations). If I use this code altered for regular fields, like CharFields or IntegerFields, it works.
EDIT2: If you have another better way of testing abstract base model that use ForeignKeys, please let me know!
setUp
.. – Doretheadorettamixin
would make it clearer.. – Doretheadoretta