#11437: [soc2009/multidb] M2M Relationship with through=[model] whose primary 
key
is renamed fails with wrong column name
------------------------------------------+---------------------------------
 Reporter:  anonymous                     |       Owner:  nobody    
   Status:  new                           |   Milestone:            
Component:  Database layer (models, ORM)  |     Version:  SVN       
 Keywords:  multidb manytomany db_column  |       Stage:  Unreviewed
Has_patch:  0                             |  
------------------------------------------+---------------------------------
 Deleting an object that is involved in a many to many relationship using
 an intermediary model whose primary key is renamed at the db level fails
 because the generated SELECT statement is wrong.

 In the example below, clicking on 'Delete' of an 'AdSalesAccount' results
 in "OperationalError: (1054, "Unknown column 'ad_order_listing_bridge.id'
 in 'field list'")"

 {{{
 Error occurs in django\db\models\sql\query.py in execute_sql on line 2370:

 Traceback:
 File "C:\Python25\lib\site-packages\django\core\handlers\base.py" in
 get_response
   87.                 response = middleware_method(request, callback,
 callback_args, callback_kwargs)
 File "build\bdist.win32\egg\firepython\middleware.py" in process_view
   305.         return self._profile_wrap(callback)(*args,
 **callback_kwargs)
 File "C:\Python25\lib\site-packages\django\contrib\admin\sites.py" in root
   480.                 return self.model_page(request, *url.split('/', 2))
 File "C:\Python25\lib\site-packages\django\views\decorators\cache.py" in
 _wrapped_view_func
   44.         response = view_func(request, *args, **kwargs)
 File "C:\Python25\lib\site-packages\django\contrib\admin\sites.py" in
 model_page
   499.         return admin_obj(request, rest_of_url)
 File "C:\Python25\lib\site-packages\django\contrib\admin\options.py" in
 __call__
   1092.             return self.delete_view(request, unquote(url[:-7]))
 File "C:\Python25\lib\site-packages\django\contrib\admin\options.py" in
 delete_view
   1009.         get_deleted_objects(deleted_objects, perms_needed,
 request.user, obj, opts, 1, self.admin_site)
 File "C:\Python25\lib\site-packages\django\contrib\admin\util.py" in
 get_deleted_objects
   125.             for sub_obj in getattr(obj, rel_opts_name).all():
 File "C:\Python25\lib\site-packages\django\db\models\query.py" in
 _result_iter
   110.                 self._fill_cache()
 File "C:\Python25\lib\site-packages\django\db\models\query.py" in
 _fill_cache
   707.                     self._result_cache.append(self._iter.next())
 File "C:\Python25\lib\site-packages\django\db\models\query.py" in iterator
   242.         for row in self.query.results_iter():
 File "C:\Python25\lib\site-packages\django\db\models\sql\query.py" in
 results_iter
   287.         for rows in self.execute_sql(MULTI):
 File "C:\Python25\lib\site-packages\django\db\models\sql\query.py" in
 execute_sql
   2370.         cursor.execute(sql, params)
 }}}
 The reason is invalid SQL is generated here:
 {{{
 sql = u'SELECT `ad_order_listing_bridge`.`id`, #####Should be ad_orderId
 `ad_order_listing_bridge`.`ad_order_listing_bridgeId`,
 `ad_order_listing_bridge`.`ad_orderId`,
 `ad_order_listing_bridge`.`listingId` FROM `ad_order_listing_bridge` WHERE
 `ad_order_listing_bridge`.`ad_orderId` = %s '}}}

 {{{
 #!python
 AdSalesModel = models.Model
 class AdSalesAccount(AdSalesModel):
     id = models.AutoField(primary_key=True, db_column='accountId')
     class Meta:
         db_table = u'account'
         ordering = ('-date_created', 'name')
         using = 'adsales'

 class AdSalesAdOrder(models.Model):
     id = models.AutoField(primary_key=True, db_column='ad_orderId')
     account = models.ForeignKey(AdSalesAccount, db_column='accountId')
     listings = models.ManyToManyField('AdSalesListing',
 through='AdSalesAdOrderListingBridge')
     class Meta:
         db_table = u'ad_order'
         ordering = ('-date_created', )
         using = 'adsales'

 class AdSalesAdOrderListingBridge(models.Model):
     id = models.IntegerField(db_column='ad_order_listing_bridgeId')
     adorder = models.ForeignKey(AdSalesAdOrder, db_column='ad_orderId')
     listing = models.ForeignKey('AdSalesListing', db_column='listingId')
     class Meta:
         using = 'adsales'
         db_table = u'ad_order_listing_bridge'

 class AdSalesListing(AdSalesModel):
     id = models.AutoField(primary_key=True, db_column='listingId')
     class Meta:
         db_table = u'listing'
         ordering = ("-date_created", "descriptive_line")
         using = 'adsales'
 }}}

-- 
Ticket URL: <http://code.djangoproject.com/ticket/11437>
Django <http://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to