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
86
votes
8 answers

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 if I don't know the name of the specific child…
Gabriel Hurley
  • 39,690
  • 13
  • 62
  • 88
83
votes
3 answers

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 problems() { return…
Rodrigo
  • 3,129
  • 3
  • 33
  • 61
82
votes
2 answers

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(targetEntity="Cart", cascade={"all"}, fetch="LAZY") …
Mohamed Ben HEnda
  • 2,686
  • 1
  • 30
  • 44
79
votes
6 answers

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, blank=True, null=True) The question is if I can use…
Ron
  • 22,128
  • 31
  • 108
  • 206
67
votes
4 answers

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; @ManyToMany @JoinTable(name = "USER_GROUP", …
Hery
  • 7,443
  • 9
  • 36
  • 41
66
votes
3 answers

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 third table called UserGroups with a UserID column,…
63
votes
4 answers

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: java.lang.UnsupportedOperationException at…
Pete
  • 10,720
  • 25
  • 94
  • 139
62
votes
2 answers

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
  • 5,768
  • 9
  • 40
  • 69
61
votes
1 answer

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 primary key from each class would link to this…
Pricey
  • 5,799
  • 12
  • 60
  • 84
59
votes
2 answers

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', db.Column('user_id', db.Integer,…
lestat
  • 1,565
  • 2
  • 14
  • 12
57
votes
2 answers

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..* } public class Contract : Entity { public string…
Kratos
  • 649
  • 2
  • 7
  • 15
55
votes
2 answers

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 %}
    {% for conference in conferences %}
  • {{ conference.date }}
Noam Smadja
  • 915
  • 2
  • 8
  • 29
53
votes
1 answer

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 have to do a raw SQL query on this table or is it…
jul
  • 36,404
  • 64
  • 191
  • 318
52
votes
3 answers

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 I can't figure out how to go about this for the…
Noel Evans
  • 8,113
  • 8
  • 48
  • 58
51
votes
4 answers

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 Table A, which has three columns: id a_id b_id But…
Wei Lin
  • 749
  • 2
  • 6
  • 9