Django 1.4 Multiple Databases Foreignkey relationship (1146, “Table 'other.orders_iorder' doesn't exist”)

自闭症网瘾萝莉.ら 提交于 2021-02-19 06:07:21

问题


I have a Foreign Key from one model into another model in a differente database (I know I shouldn't do it but if I take care properly of Referential Integrity it shouldn't be a problem).

The thing is that everything works fine...all the system does (relationships on any direction, the router takes care of it) but when I try to delete the referenced model (which doesn't have the foreign key attribute)...Django still wants to go throught the relationship to check if the relationship is empty, but the related object is on another database so it doesn't find the object in this database.

I tried to set up on_delete=models.DO_NOTHING with no success. Also tried to clear the relationship (but it happens clear doesn't have "using" argument so I it doesn't work either). Also tried to empty the relationship with delete(objects...), no success.

Now I am pretty sure the problem is in super(Object,self).delete(), I can not do super(Object,self).delete(using=other_database) because the self object is not in another database just the RelatedManager is. So I don't know how to make Django to understand I don't want even to check that relationship, which by the way was already emptied before the super(Object,self).delete() request.

I was thinking if there is some method I can override to make Django avoid this check.

More graphical:

DB1: "default" database (orders app)

from django.db import models from shop.models import Order

class IOrder(models.Model):

name = models.CharField(max_length=20, unique=True, blank=False, null=False)
order = models.ForeignKey(Order, related_name='iorders', blank=True, null=True)

DB2: "other" database

class Order(models.Model):

description = models.CharField(max_length=20, blank=False, null=False)

def delete(self): 
    # Delete iOrder if any
    for iorder in self.iorders.using('default'):
        iorder.delete()

    # Remove myself
    super(Order, self).delete()

The problem happens when supper(Order.self).delete() is called, then it can not find the table (iorder) in this database (because it is in 'default')

Some idea? Thanks in advance,


回答1:


I already resolved my issue changing super(Order,self).delete() with a raw SQL delete command. Anyway I would love to know if there is a more proper way of doing this



来源:https://stackoverflow.com/questions/11827272/django-1-4-multiple-databases-foreignkey-relationship-1146-table-other-order

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!