#13668 closed (fixed)
ManyToManyField passes ModelBase to router.db_for_write
| Reported by: | Owned by: | David Gouldin | |
|---|---|---|---|
| Component: | Database layer (models, ORM) | Version: | 1.2 |
| Severity: | Keywords: | ||
| Cc: | dgouldin@… | 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
When using a ManyToMany field without the through parameter, the incorrect model type is passed into the router functions.
Consider the following example:
Models:
from django.db import models
class A(models.Model):
asdf = models.CharField(max_length=10)
class B(models.Model):
b = models.CharField(max_length=10)
asdfs = models.ManyToManyField('A')
Router:
class MyRouter(object):
def db_for_write(self, model, **hints):
if model._meta.app_label == 'model_app_label':
return 'not_default'
def db_for_read(self, model, **hints):
if model._meta.app_label == 'model_app_label'
return 'not_default'
def allow_relation(self, obj1, obj2, **hints):
return obj1._state.db == obj2._state.db
def allow_syncdb(self, db, model):
if db == 'not_default' and model._meta.app_label == 'model_app_label':
return True
if db == 'default' and model._meta.app_label == 'model_app_label':
return False
return None
Running the following bit of code:
aa = A.objects.create(asdf='1')
bb = B.objects.create(b='asdf')
bb.asdfs.add(aa)
I get an error about how the intermediary table model_app_label_b_asdfs on the default database does not exist
Attachments (1)
Change History (8)
comment:1 by , 15 years ago
| Component: | Uncategorized → Database layer (models, ORM) |
|---|---|
| milestone: | → 1.3 |
| Triage Stage: | Unreviewed → Accepted |
comment:2 by , 15 years ago
| Cc: | added |
|---|---|
| Owner: | changed from to |
by , 15 years ago
| Attachment: | 13668.diff added |
|---|
comment:3 by , 15 years ago
| Has patch: | set |
|---|
comment:4 by , 15 years ago
| Triage Stage: | Accepted → Ready for checkin |
|---|
comment:5 by , 15 years ago
| Resolution: | → fixed |
|---|---|
| Status: | new → closed |
comment:6 by , 15 years ago
Note:
See TracTickets
for help on using tickets.
Looks good to me. Thanks.