﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
13668	ManyToManyField passes ModelBase to router.db_for_write	craig.kimerer@…	David Gouldin	"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

"		closed	Database layer (models, ORM)	1.2		fixed		dgouldin@…	Ready for checkin	1	0	0	0	0	0
