Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

baker.make tries to set a field that is a foreign key on ContentType that something that doesn't exist. #341

Open
Salaah01 opened this issue Aug 24, 2022 · 1 comment

Comments

@Salaah01
Copy link

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

from django.db import models
from django.contrib.contenttypes.models import ContentType


class MyModel(models.Model):
    related_object_type = models.ForeignKey(
        to=ContentType,
        on_delete=models.PROTECT,
    )

Within the tests somewhere:

class TestUserMatView:
   
    username = models.CharField(max_length=64)
    
    class Meta:
        db_table = "test_user_mat_view"
        managed = False

    @classmethod
    def create_view(cls) -> None:
        cls.drop_view()  # Ensures any previous creations will be torn down.
        with connection.cursor() as cursor:
            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)],
            )

    @classmethod
    def drop_view(cls) -> None:
        with connection.cursor() as cursor:
            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).
@berinhard
Copy link
Member

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:

  1. The traceback of the error;
  2. 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants