Opened 6 weeks ago

Last modified 6 weeks ago

#31762 new Bug

Dabase creation backend should use base_manager to serialize database

Reported by: Eugene K Owned by: nobody
Component: Testing framework Version: 3.0
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: yes Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

models.py

class Service(Model):
    objects = CustomManagerThatFillterOutSomeRecords()

class CustomManagerThatFillterOutSomeRecords(Manager):
    def get_queryset(self):
        return super().get_queryset().exclude(pk=1)

tests.py

class TestService(TransactionTestCase):
    serialized_rollback = True

    def test_something(self):
        pass

Assume we have a migration that creates few records of Service.

from django.core.management import call_command
from django.db import migrations


def load_fixtures(*_, **__):
    call_command('loaddata', 'services.json')


class Migration(migrations.Migration):
    dependencies = []

    operations = [
        migrations.RunPython(
            load_fixtures,
            migrations.RunPython.noop,
        )
    ]

Then TestService will fail as serialize_db_to_string by default use _default_manager that is CustomManagerThatFillterOutSomeRecords.

Here is proposed fix: https://github.com/django/django/pull/13150

Change History (1)

comment:1 Changed 6 weeks ago by Simon Charette

Component: UncategorizedTesting framework
Has patch: set
Needs tests: set
Triage Stage: UnreviewedAccepted
Type: UncategorizedBug

That makes sense.

You'll need to add regression tests to your PR in [django/db/backends/base/creation.py https://github.com/django/django/blob/4d9cd89acbb944e10b9000092069ba8e3a855957/django/db/backends/base/creation.py] by defining a default manager that exclude rows django/db/backends/models.py, creating one such row, and ensure it's part of the string returned by serialize_db_to_string.

Note: See TracTickets for help on using tickets.
Back to Top