ManyToManyField error when having recursive structure. How to solve it?

Posted by luc on Stack Overflow See other posts from Stack Overflow or by luc
Published on 2010-05-07T14:08:52Z Indexed on 2010/05/07 14:28 UTC
Read the original article Hit count: 180

Hello,

I have the following table in the model with a recursive structure (a page can have children pages)

class DynamicPage(models.Model):    
    name = models.CharField("Titre",max_length=200)
    parent = models.ForeignKey('self',null=True,blank=True)

I want to create another table with manytomany relation with this one:

class UserMessage(models.Model):    
    name = models.CharField("Nom", max_length=100)
    page = models.ManyToManyField(DynamicPage)

The generated SQL creates the following constraint:

ALTER TABLE `website_dynamicpage` ADD CONSTRAINT `parent_id_refs_id_29c58e1b` FOREIGN KEY (`parent_id`) REFERENCES `website_dynamicpage` (`id`); 

I would like to have the ManyToMany with the page itself (the id) and not with the parent field.

How to modify the model to make the constraint using the id and not the parent?

Thanks in advance

© Stack Overflow or respective owner

Related posts about python

Related posts about django