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
50
votes
4 answers

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) for model in get_models(app): print…
jack
  • 17,261
  • 37
  • 100
  • 125
50
votes
1 answer

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 many table GroupID | ElementID ----------------- …
Martin Tramšak
  • 607
  • 1
  • 7
  • 12
49
votes
3 answers

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, sometimes you may need to associate data with the…
Alex
  • 2,350
  • 2
  • 20
  • 17
49
votes
6 answers

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 relational model: I need a list of Articles for a…
D.B
  • 4,009
  • 14
  • 46
  • 83
48
votes
5 answers

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 simple example?
good_evening
  • 21,085
  • 65
  • 193
  • 298
48
votes
11 answers

How to sort NULL values last using Eloquent in Laravel

I've got a many to many relationship between my employees and groups table. I've created the pivot table, and all is working correctly with that. However, I've got a sortOrder column on my employees table that I use to determine the order in which…
eagle0042
  • 507
  • 2
  • 7
  • 12
48
votes
3 answers

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 every user. Is it possible? The exact thing I want to do is…
CastleDweller
  • 8,204
  • 13
  • 49
  • 69
47
votes
3 answers

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 keys. I have a table of items and every item has…
duduklein
  • 10,014
  • 11
  • 44
  • 55
46
votes
5 answers

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 Friends { get; set; } [Key] public int MemberId { get; set; } public string Name{ get; set; } } I tried adding Mappings but in vain. Is there a way to…
Korayem
  • 12,108
  • 5
  • 69
  • 56
46
votes
6 answers

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 to be detached from the associated products in the…
Grant J
  • 1,056
  • 3
  • 10
  • 18
45
votes
1 answer

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 Library2Books { get; set; } } public class Book { [Key] …
Yurii N.
  • 5,455
  • 12
  • 42
  • 66
43
votes
1 answer

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.objects.get(pk=code) except: article =…
Totty.js
  • 15,563
  • 31
  • 103
  • 175
43
votes
1 answer

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('follower_id', db.Integer,…
42
votes
3 answers

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 make a composite primary index…
serg
  • 109,619
  • 77
  • 317
  • 330
42
votes
2 answers

@ManyToMany(mappedBy = "foo")

Foo has: @ManyToMany(mappedBy = "foos") private Set bars and Bar has : @ManyToMany private Set foos What difference does the location of mappedBy attribute make to a bi-directional relationship , other than whether table is called…
NimChimpsky
  • 46,453
  • 60
  • 198
  • 311