Questions tagged [many-to-many]

A type of relationship between entities of types A and B which associates a list of entities of type B to an entity of type A and vice versa. Types A and B may be the same type.

A type of relationship between entities of types A and B which associates a list of entities of type B to an entity of type A and vice versa. Types A and B may be the same type.

8687 questions
656
votes
6 answers

What is related_name used for?

What is the related_name argument useful for on ManyToManyField and ForeignKey fields? For example, given the following code, what is the effect of related_name='maps'? class Map(db.Model): members = models.ManyToManyField(User,…
zjm1126
  • 63,397
  • 81
  • 173
  • 221
327
votes
7 answers

Create code first, many to many, with additional fields in association table

I have this scenario: public class Member { public int MemberID { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public virtual ICollection Comments { get; set; } } public class…
hgdean
  • 3,273
  • 3
  • 16
  • 5
306
votes
2 answers

How do I make many-to-many field optional in Django?

When you have a many-to-many relationship (related_name, not through) and you are trying to use the admin interface you are required to enter one of the relationships even though it does not have to exist for you to create the first entry. I'm…
DZ.
  • 3,181
  • 2
  • 15
  • 10
216
votes
8 answers

Difference Between One-to-Many, Many-to-One and Many-to-Many?

Ok so this is probably a trivial question but I'm having trouble visualizing and understanding the differences and when to use each. I'm also a little unclear as to how concepts like uni-directional and bi-directional mappings affect the…
Ian Dallas
  • 12,451
  • 19
  • 58
  • 82
210
votes
5 answers

Rails find_or_create_by more than one attribute?

There is a handy dynamic attribute in active-record called find_or_create_by: Model.find_or_create_by_(: => "") But what if I need to find_or_create by more than one attribute? Say I have a model to handle a M:M relationship…
tybro0103
  • 48,327
  • 33
  • 144
  • 170
191
votes
5 answers

In which case do you use the JPA @JoinTable annotation?

In which case do you use the JPA @JoinTable annotation?
kostas trichas
  • 2,923
  • 7
  • 28
  • 37
176
votes
4 answers

Django removing object from ManyToMany relationship

How would I delete an object from a Many-to-Many relationship without removing the actual object? Example: I have the models Moods and Interest. Mood has a many-to-many field interests (which is a models.ManyToManyField(Interest)). I create an…
Nachshon Schwartz
  • 15,289
  • 20
  • 59
  • 98
164
votes
5 answers

MongoDB Many-to-Many Association

How would you do a many-to-many association with MongoDB? For example; let's say you have a Users table and a Roles table. Users have many roles, and roles have many users. In SQL land you would create a UserRoles table. Users: Id …
Josh Close
  • 22,935
  • 13
  • 92
  • 140
154
votes
5 answers

SQL - many-to-many table primary key

This question comes up after reading a comment in this question: Database Design When you create a many-to-many table, should you create a composite primary key on the two foreign key columns, or create a auto-increment surrogate "ID" primary key,…
Andy White
  • 86,444
  • 48
  • 176
  • 211
153
votes
1 answer

How to implement a many-to-many relationship in PostgreSQL?

I believe the title is self-explanatory. How do you create the table structure in PostgreSQL to make a many-to-many relationship. My example: Product(name, price); Bill(name, date, Products);
Radu Gheorghiu
  • 20,049
  • 16
  • 72
  • 107
145
votes
3 answers

Mapping many-to-many association table with extra column(s)

My database contains 3 tables: User and Service entities have many-to-many relationship and are joined with the SERVICE_USER table as follows: USERS - SERVICE_USER - SERVICES SERVICE_USER table contains additional BLOCKED column. What is the best…
archie_by
  • 1,623
  • 2
  • 11
  • 10
117
votes
5 answers

Insert/Update Many to Many Entity Framework . How do I do it?

I'm using EF4 and new to it. I have a many to many in my project and cannot seem to work out how to insert or update. I have build a small project just to see how it should be coded. Suppose I have 3 tables Class : ClassID-ClassName Student :…
user9969
  • 15,632
  • 39
  • 107
  • 175
111
votes
4 answers

Django filter many-to-many with contains

I am trying to filter a bunch of objects through a many-to-many relation. Because the trigger_roles field may contain multiple entries I tried the contains filter. But as that is designed to be used with strings I'm pretty much helpless how i should…
Grave_Jumper
  • 1,224
  • 2
  • 8
  • 11
103
votes
2 answers

Whats the difference between a OneToOne, ManyToMany, and a ForeignKey Field in Django?

I'm having a little difficulty getting my head around relationships in Django models. Could someone explain what the difference is between a OneToOne, ManyToMany and ForeignKey?
94
votes
1 answer

Django's ManyToMany Relationship with Additional Fields

I want to store some additional information in that, automatically created, ManyToMany join-table. How would I do that in Django? In my case I have two tables: "Employees" and "Projects". What I want to store is how much each of the employees…
rrb_bbr
  • 2,966
  • 4
  • 24
  • 26
1
2 3
99 100