0

My model :

class Image(models.Model):
    name=models.CharField(max_length=40,unique=True,help_text="name of the image")
    tags = models.ManyToManyField(Tag)
class Tag(models.Model):
    tag = models.CharField(max_length=100,unique=True)

here when I do makemigrations and migrate it is creating 3 tables inside my database 1.image 2.tag 3.image_tags table

so, my question is i am not specifying image_tags table in my models.py file ,from where django is creating image_tags table and what is the flow ?? I have checked in migrations file but I didnot get any clarity regarding this

L Shafiya
  • 31
  • 7

2 Answers2

0

An intermediary table is required for a Many-To-Many relationship in a database, and because most of the time you don't need to store extra data on the relationship, Django just silently creates this table for you. In your case it will create a table with 3 fields: id, image_id, tag_id.

If you want to specify your own intermediary table, for example if you want to store extra data, you can create a model with ForeignKey's to your related tables and then define your ManyToManyField with a "through" argument like so:

class ImageTag(models.Model):
  image = models.ForeignKey('Image')
  tag = models.ForeignKey('Tag')
  extra_data = models.CharField()

class Tag(models.Model):
  name = models.CharField()

class Image(models.Model):
  name = models.CharField()
  tags = models.ManyToManyField(Tag, through=ImageTag)
AzMoo
  • 486
  • 3
  • 10
  • Thanks for the information, but do you know like how django handles creating that internal table – L Shafiya Mar 13 '19 at 06:55
  • It does it the same way it creates all other tables. When migrations are run it executes the CreateModel operation, which iterates through the fields in the table. When it comes across a ManyToManyField the database backend executes the "create_model" method taking the generated "through" model as its argument. The relevant code in the Django source is here: https://github.com/django/django/blob/782d85b6dfa191e67c0f1d572641d8236c79174c/django/db/backends/base/schema.py#L316 – AzMoo Mar 13 '19 at 23:15
0

Actually, that is not Django Functionality. It's SQL functionality. SQL is creating the internal table. Because SQL can't create reference, it's not Foreign key. That's why this Bridging table concept is emerged. It will resolve the problem as well as it will hold data(ID) of both tables and few custom fields, depends on requirements.

Updated with new req: Refer this:

https://gist.github.com/jacobian/827937

Django 1.8 - Intermediary Many-to-Many-Through Relationship - What is the consequence of where 'ManytoManyField' is used?

From my point of view, It's not good practice to customize the bridging table. You can specify the extra fields to any of two tables orelse create a new table make it as foreign key

Sakthi Panneerselvam
  • 1,337
  • 1
  • 14
  • 25
  • ok,thanks.... but my case is i wanted to create a custom field similar to many-to-many so i wanted to know how exactly Django is handling creation of that internal table so that i can also do the same – L Shafiya Mar 13 '19 at 10:09