0

I have two models A and B where

class B(Model):
   As = models.ManyToManyField(A)

Then I do the following

a_instance.b_set.clear()

in order to remove all references from a_instance to any b_instances. But then I've got the following error:

Database Error: column a_b/id doesn not exists
Line 1:  SELECT "a_b"."id",

And this is true, my intermediate table a_b in many-to-many relationship (between A and B) doesn't have field named id. It has two other fields instead a_id and b_id.

So does anyone knows how can I force Django to use a_id and b_id instead of just id?

Pavel Podlipensky
  • 8,201
  • 5
  • 42
  • 53

2 Answers2

0

If you need to delete the relationship for one instance between 2 models you can do that by accessing the Manager of the relationship table. The m2m relationship table can be accessed via MyModel.relations.through so for deleting the relationships:

B.As.through.objects.filter(a=a_instance).delete()

reference:

https://docs.djangoproject.com/en/dev/ref/models/fields/#django.db.models.ManyToManyField.through

Credit : How to remove all relations from manytomany?

Community
  • 1
  • 1
Benjamin Toueg
  • 10,511
  • 7
  • 48
  • 79
  • thanks for clarification, but now I receive exactly the same error I had before - "no id field in the a_b table". Any other ideas of why it might happen? – Pavel Podlipensky Oct 29 '13 at 23:18
  • It is very strange that your intermediary table does not have an `id` column. Just checked on my db and it should. Have you tried customizing this table through Django? Or maybe directly with SQL? – Benjamin Toueg Oct 29 '13 at 23:24
0

Django requires to have id field in many-to-many relationship table.

Pavel Podlipensky
  • 8,201
  • 5
  • 42
  • 53