steve-gregory
steve-gregory

Reputation: 7466

How do I express a Django ManyToMany relationship?

I'm hitting a wall here and I know this is a simple question, but I was unable to find it here.

In an ER diagram, what would the relationship be between two objects that have a ManyToMany relationship, in terms of the intermediary table?

Example:
item ---- item_facts ---- fact

I feel like it should be one to one but I'm not completely sure.

Upvotes: 0

Views: 313

Answers (3)

Mp0int
Mp0int

Reputation: 18737

In django documentation it states that

A many-to-many relationship. Requires a positional argument: the class to which the model is related. This works exactly the same as it does for ForeignKey, including all the options regarding recursive and lazy relationships.

Behind the scenes, Django creates an intermediary join table to represent the many-to-many relationship. By default, this table name is generated using the name of the many-to-many field and the model that contains it. Since some databases don't support table names above a certain length, these table names will be automatically truncated to 64 characters and a uniqueness hash will be used. This means you might see table names like author_books_9cdf4; this is perfectly normal. You can manually provide the name of the join table using the db_table option.

And ForeignKey definition is like:

A many-to-one relationship. Requires a positional argument: the class to which the model is related.

So,ManyToMany relations created by django are creating intermedıary tables that are 1 to N.

Upvotes: 0

user --many2many-- group

user  1----n user_group n---1 group

Upvotes: 1

ypercubeᵀᴹ
ypercubeᵀᴹ

Reputation: 115630

Not sure what the question is here. You say that the two objects have a many-to-many relationship.

If two objects (entitied, tables) have a many-to-many relationship, whether you include the intermediate table in the diagram or not, is irrelevant. They still have a many-to-many relationship.

Upvotes: 0

Related Questions