Hi all,

I using django 1.2
I have a problem with model and Foreign Key on self table.

I try to explain the problem with an example:

I have one table like this :

class Test_obj ( models.Model ):
    name = models.CharField(max_length=255, unique=True, null=True)
    use = models.ForeignKey('self', to_field='name',null=True,
db_column='use')

    class Meta:
        abstract = True


class Test ( Test_obj ):
    host_name =              models.CharField(max_length=255,
unique=True, null=True)
    alias =                  models.CharField(max_length=255,
null=True)

    def __unicode__(self):
        if self.name:
            return self.name
        else:
            return self.host_name


Now, in shell, I create some objects and I delete a parent object
"template1" and django works as I'm expect emulating
on delete cascade :

In [8]: template1 = Test(name='template1')

In [9]: template1.save()

In [10]: host1 = Test(host_name='host1')

In [11]: host2 = Test(host_name='host2')

In [12]: host3 = Test(host_name='host3')

In [13]: host1.use = template1

In [14]: host2.use = template1

In [15]: host3.use = template1

In [16]: host1.save()

In [17]: host2.save()

In [18]: host3.save()

In [19]: Test.objects.all()
Out[19]: [<Test: template1>, <Test: host1>, <Test: host2>, <Test:
host3>]

In [20]: template1.delete()

In [21]: Test.objects.all()
Out[21]: []


But I don't understand why django do the same thing when I delete a
children :

In [27]: Test.objects.all()
Out[27]: [<Test: host1>, <Test: host2>, <Test: host3>, <Test:
template1>]

In [28]: hos1 = Test.objects.get(host_name='host1')

In [29]: host1.delete()

In [30]: Test.objects.all()
Out[30]: []


Is this a django bug or I'm wrong something ?

Thank you,
Dave

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Reply via email to