Opened 5 years ago
Closed 5 years ago
#31762 closed Bug (fixed)
Dabase creation backend should use base_manager to serialize database
| Reported by: | Eugene K | Owned by: | Hasan Ramezani |
|---|---|---|---|
| Component: | Testing framework | Version: | 3.0 |
| Severity: | Normal | Keywords: | |
| Cc: | Triage Stage: | Ready for checkin | |
| Has patch: | yes | Needs documentation: | no |
| Needs tests: | no | 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 (4)
comment:1 by , 5 years ago
| Component: | Uncategorized → Testing framework |
|---|---|
| Has patch: | set |
| Needs tests: | set |
| Triage Stage: | Unreviewed → Accepted |
| Type: | Uncategorized → Bug |
comment:2 by , 5 years ago
| Needs tests: | unset |
|---|---|
| Owner: | changed from to |
| Status: | new → assigned |
comment:3 by , 5 years ago
| Triage Stage: | Accepted → Ready for checkin |
|---|
Note:
See TracTickets
for help on using tickets.
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 byserialize_db_to_string.