You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Describe the issue
As part of testing an abstract model, within the tests, we create a dummy model which is used for testing and then is later dropped. This dummy model represents a view.
In other models we have fields which are a foreign key on the content type model. The problem arises when we try to use baker.make to create an instance of that model. Most of the time it works fine, but sometimes, baker tries to set the content type field to the dummy view content type we had previously dropped.
classTestUserMatView:
username=models.CharField(max_length=64)
classMeta:
db_table="test_user_mat_view"managed=False@classmethoddefcreate_view(cls) ->None:
cls.drop_view() # Ensures any previous creations will be torn down.withconnection.cursor() ascursor:
cursor.execute(
"""CREATE MATERIALIZED VIEW IF NOT EXISTS %s AS SELECT * FROM auth_user; """,
[AsIs(cls._meta.db_table)],
)
cursor.execute(
"CREATE UNIQUE INDEX ON %s (username);",
[AsIs(cls._meta.db_table)],
)
@classmethoddefdrop_view(cls) ->None:
withconnection.cursor() ascursor:
cursor.execute(
"DROP MATERIALIZED VIEW IF EXISTS %s",
[AsIs(cls._meta.db_table)],
)
Create some tests that involve running TestUserMatView.create_view and then later TestUserMatView.drop_view (to clean). Then, create tests where we use baker.make(MyModel).
This should work a couple of times, but at some point, there should be an error that moans about the table test_user_mat_view not existing.
Expected behavior
It should create the instance without a hitch.
Versions
Python: 3.9.5
Django: 3.2.11
Model Bakery: 1.0.0 (we can't use a newer version due to a memory leak issue that I've raised).
The text was updated successfully, but these errors were encountered:
Hi @Salaah01 thanks for opening this, but I don't see how model bakery can be causing any error. On your issue there's no traceback from an error or even a code snippet showing model-bakery being used. Also, it's clear that your scenario is a little bit more complex, since your Django app depends on materialized views. This, for example, can have a collateral effect with the error you're seeing.
I mean, can you provide:
The traceback of the error;
A complete python test code we can use to test;
It would make our life, as maintainers, easier to seek were the error happen and to be able to reproduce your scenario. Thanks.
Describe the issue
As part of testing an abstract model, within the tests, we create a dummy model which is used for testing and then is later dropped. This dummy model represents a view.
In other models we have fields which are a foreign key on the content type model. The problem arises when we try to use
baker.make
to create an instance of that model. Most of the time it works fine, but sometimes, baker tries to set the content type field to the dummy view content type we had previously dropped.To Reproduce
models.py
Within the tests somewhere:
Create some tests that involve running
TestUserMatView.create_view
and then laterTestUserMatView.drop_view
(to clean). Then, create tests where we usebaker.make(MyModel
).This should work a couple of times, but at some point, there should be an error that moans about the table
test_user_mat_view
not existing.Expected behavior
It should create the instance without a hitch.
Versions
The text was updated successfully, but these errors were encountered: