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.

Filter by
Sorted by
Tagged with
677 votes
6 answers
443k views

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): ...
zjm1126's user avatar
  • 65.1k
329 votes
7 answers
154k views

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&...
hgdean's user avatar
  • 3,293
310 votes
2 answers
129k views

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 ...
DZ.'s user avatar
  • 3,221
223 votes
8 answers
214k views

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 ...
Ian Dallas's user avatar
  • 12.6k
210 votes
5 answers
119k views

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_<attribute>(:<attribute> => "") But what if I need to find_or_create by more than ...
tybro0103's user avatar
  • 49.1k
194 votes
5 answers
273k views

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

In which case do you use the JPA @JoinTable annotation?
kostas trichas's user avatar
187 votes
4 answers
170k views

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 ...
Nachshon Schwartz's user avatar
164 votes
5 answers
102k views

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 ...
Josh Close's user avatar
  • 23.1k
160 votes
1 answer
162k views

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's user avatar
156 votes
5 answers
73k views

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 ...
Andy White's user avatar
  • 87.4k
143 votes
3 answers
132k views

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 ...
archie_by's user avatar
  • 1,613
117 votes
5 answers
148k views

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 ...
user9969's user avatar
  • 15.8k
113 votes
4 answers
114k views

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 ...
Grave_Jumper's user avatar
  • 1,244
104 votes
2 answers
42k views

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?
user3915903's user avatar
  • 1,195
97 votes
1 answer
56k views

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 ...
rrb_bbr's user avatar
  • 3,026
86 votes
3 answers
129k views

getting the value of an extra pivot table column laravel

I have a phone_models, phone_problems, and a phone_model_phone_problem pivot table. The pivot table has an extra column 'price'. PhoneModel: class PhoneModel extends \Eloquent { public function ...
Rodrigo's user avatar
  • 3,199
86 votes
7 answers
37k views

How do I access the child classes of an object in django without knowing the name of the child class?

In Django, when you have a parent class and multiple child classes that inherit from it you would normally access a child through parentclass.childclass1_set or parentclass.childclass2_set, but what ...
Gabriel Hurley's user avatar
84 votes
2 answers
96k views

What is the difference between fetch="EAGER" and fetch="LAZY" in doctrine

What is the difference between fetch="EAGER" and fetch="LAZY" in annotation @ManyToOne in Doctrine ? /** * @ManyToOne(targetEntity="Cart", cascade={"all"}, fetch="EAGER") */ /** * @ManyToOne(...
Mohamed Ben HEnda's user avatar
81 votes
6 answers
46k views

Django Many-to-Many (m2m) Relation to same model

I'd like to create a many-to-many relationship from and to a user class object. I have something like this: class MyUser(models.Model): ... blocked_users = models.ManyToManyField(MyUser, ...
Ron's user avatar
  • 22.9k
68 votes
4 answers
36k views

UnsupportedOperationException merge-saving many-to-many relation with hibernate and JPA

I've set up a simple many-to-many relationship account : role with Hibernate but when I try to save an account in a unit test after it has had its role added I get an UnsupportedOperationException: ...
Pete's user avatar
  • 10.8k
67 votes
4 answers
83k views

JPA Hibernate many-to-many cascading

I am using JPA 2.0 and hibernate. I have a User class and a Group class as follows: public class User implements Serializable { @Id @Column(name="USER_ID") private String userId; @...
Hery's user avatar
  • 7,559
66 votes
3 answers
32k views

How to organise a many to many relationship in MongoDB

I have two tables/collections; Users and Groups. A user can be a member of any number of groups and a user can also be an owner of any number of groups. In a relational database I'd probably have a ...
Typo Johnson's user avatar
  • 6,014
62 votes
2 answers
55k views

Symfony2-Doctrine: ManyToMany relation is not saved to database

I have two PHP model classes named Category and Item. A Category may have many Items and an Item may belong to many Categories. I have created a ManyToMany relation to both classes: class Category { ...
Omid Kamangar's user avatar
61 votes
1 answer
65k views

Many-to-many mapping table

From examples that I have seen online and in a Programming Entity Framework CodeFirst book, when you have a collection on both classes EF would create a mapping table such as MembersRecipes and the ...
Pricey's user avatar
  • 5,859
59 votes
2 answers
29k views

SQLAlchemy ManyToMany secondary table with additional fields

I have 3 tables: User, Community, community_members (for relationship many2many of users and community). I create this tables using Flask-SQLAlchemy: community_members = db.Table('community_members'...
lestat's user avatar
  • 1,575
57 votes
2 answers
82k views

How to create a many-to-many mapping in Entity Framework?

Here is the case, I have 2 entities, such as Contract、Media。 public class Media : Entity { public string Name {get; set;} public bool Enabled *//other properties can be ignored..* } ...
Kratos's user avatar
  • 649
57 votes
1 answer
17k views

How to query directly the table created by Django for a ManyToMany relation?

I have a model MyModel2 with a ManyToManyField related to another model MyModel1. How can I get the pairs mymodel1.id, mymodel2.id, as represented in the table Django creates for this relation? Do I ...
jul's user avatar
  • 36.9k
55 votes
2 answers
49k views

listing objects from ManyToManyField

I am trying to print a list of all the Conferences and for each conference, print its 3 Speakers. In my template I have: {% if conferences %} <ul> {% for conference in ...
Noam Smadja's user avatar
  • 1,015
52 votes
4 answers
29k views

How to add column in ManyToMany Table (Django)

From the example of Django Book, I understand if I create models as following: from xxx import B class A(models.Model): b = ManyToManyField(B) The Django would create a new table(A_B) beyond ...
Wei Lin's user avatar
  • 759
52 votes
3 answers
59k views

How to build many-to-many relations using SQLAlchemy: a good example

I have read the SQLAlchemy documentation and tutorial about building many-to-many relation but I could not figure out how to do it properly when the association table contains more than the 2 foreign ...
duduklein's user avatar
  • 10.3k
52 votes
3 answers
21k views

Generic many-to-many relationships

I'm trying to create a messaging system where a message's sender and recipients can be generic entities. This seems fine for the sender, where there is only object to reference (GenericForeignKey) but ...
Noel Evans's user avatar
  • 8,328
51 votes
4 answers
41k views

Get all table names in a Django app

How to get all table names in a Django app? I use the following code but it doesn't get the tables created by ManyToManyField from django.db.models import get_app, get_models app = get_app(app_name) ...
jack's user avatar
  • 17.6k
51 votes
1 answer
143k views

SQL JOIN many-to-many

Sorry about the minimalistic title but I don't know how to describe it in short. I have three tables: The table of groups ID | Genre ----------------- 1 | Action 2 | Adventure 3 | Drama Many to ...
Martin Tramšak's user avatar
50 votes
5 answers
77k views

Many-to-many relationships examples

I haven't found any MYSQL many-to-many relationships examples here and in google. What I am looking is to see a very simple example with php+mysql showing database's results. Can anybody write a very ...
good_evening's user avatar
  • 21.4k
50 votes
6 answers
58k views

Query a many-to-many relationship with linq/Entity Framework. CodeFirst

How can I query a many-to-many relationship using Entity Framework code first and linq? The problem is that EF create automatically the relation table. So, I don't have it in my context. This is the ...
D.B's user avatar
  • 4,169
50 votes
3 answers
36k views

How do I access the properties of a many-to-many "through" table from a django template?

From the Django documentation... When you're only dealing with simple many-to-many relationships such as mixing and matching pizzas and toppings, a standard ManyToManyField is all you need. However, ...
Alex's user avatar
  • 2,380
49 votes
12 answers
53k views

How to sort NULL values last using Eloquent in Laravel

I've got a many-to-many relationship between my employees and the group table. I've created the pivot table, and all is working correctly with that. However, I've got a sortOrder column on my ...
eagle0042's user avatar
  • 517
48 votes
3 answers
89k views

Get objects from a many to many field

I have an m2m field called "admins" inside a model and I need to obtain from a view all the selected entries in that field, which are user IDs. Then with the user IDs obtain the emails for ...
CastleDweller's user avatar
46 votes
6 answers
79k views

Laravel Eloquent ORM - Many to Many Delete Pivot Table Values left over

Using Laravel, I have the following code $review = Review::find(1); $review->delete(); Review has a many to many relationship defined with a Product entity. When I delete a review, I'd expect it ...
Grant J's user avatar
  • 1,065
46 votes
5 answers
20k views

Self-referencing many-to-many recursive relationship code first Entity Framework

I can't seem to make this work at all class Member { public virtual IList<Member> Friends { get; set; } [Key] public int MemberId { get; set; } public string Name{ get; set; } } ...
Korayem's user avatar
  • 12.3k
45 votes
1 answer
40k views

Saving many-to-many relationship in Entity Framework Core

For example, I have 3 classes, which I'm using for many-to-many relationship: public class Library { [Key] public string LibraryId { get; set; } public List<Library2Book> ...
Yurii N.'s user avatar
  • 5,573
44 votes
3 answers
21k views

How to properly index a linking table for many-to-many connection in MySQL?

Lets say I have a simple many-to-many table between tables "table1" and "table2" that consists from two int fields: "table1-id" and "table2-id". How should I index this linking table? I used to just ...
serg's user avatar
  • 110k
43 votes
1 answer
40k views

AttributeError: 'ManyRelatedManager' object has no attribute 'add'? I do like in django website but got this error

for item in data: category_id = item['category_id'] del item['category_id'] category = Category.objects.get(pk=category_id) code = item['code'] try: article = Article....
Totty.js's user avatar
  • 15.7k
43 votes
1 answer
18k views

Flask/SQLAlchemy - Difference between association model and association table for many-to-many relationship?

I started learning this stuff from the Flask Mega Tutorial. When he gets into Many-to-Many relationships, he creates an association table like this: followers = db.Table('followers', db.Column('...
Chockomonkey's user avatar
  • 4,025
42 votes
2 answers
34k views

@ManyToMany(mappedBy = "foo")

Foo has: @ManyToMany(mappedBy = "foos") private Set<Bar> bars and Bar has : @ManyToMany private Set<Foo> foos What difference does the location of mappedBy attribute make to a bi-...
NimChimpsky's user avatar
  • 46.9k
42 votes
1 answer
5k views

Search Core Data for all objects with an empty "to-many" relationship

In an almost identical situation to Core Data - Fetch all objects NOT in a relationship, only I'm looking for all records of one type that are not in any to-many relationship with another type. So let'...
bryanjclark's user avatar
  • 6,364
41 votes
3 answers
44k views

how to verify if object exist in manytomany

Models class Beer(models.Model): pass class Salas(models.Model): name = models.CharField(max_length=20) beers = models.ManyToManyField('Beer', blank=True) View beer = Beer.objects....
Henry Ramos's user avatar
41 votes
1 answer
53k views

How do I work with many-to-many relations in Yii2

For example in one-to-many due to documentation (http://www.yiiframework.com/doc-2.0/guide-db-active-record.html#relational-data) you can link two models in this way (one-many = company-zone): $...
raiym's user avatar
  • 1,499
40 votes
3 answers
98k views

MS SQL creating many-to-many relation with a junction table

I'm using Microsoft SQL Server Management Studio and while creating a junction table should I create an ID column for the junction table, if so should I also make it the primary key and identity ...
ab1428x's user avatar
  • 804
40 votes
20 answers
19k views

Java method naming conventions: Too many getters

Why do Java method names use the "get" prefix so extensively? At least in my Java programs there are a lot of methods with names starting with the word "get". The percentage of get-methods is ...
Are Husby's user avatar
  • 2,098

1
2 3 4 5
176