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
41
votes
1 answer

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's say I've got a set of patients, and a set of…
bryanjclark
  • 6,247
  • 2
  • 35
  • 68
41
votes
1 answer

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): $defaultZone = new Zone; $defaultZone->name =…
raiym
  • 1,439
  • 1
  • 28
  • 54
40
votes
4 answers

how to query many-to-many?

I found the following table structures while I was watching ruby on rails tutorial. table actors id int 11 primary key auto_increment name varchar 30 table movies id int 11 primary key auto_increment name varchar 30 table…
Moon
  • 22,195
  • 68
  • 188
  • 269
40
votes
20 answers

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 suspiciously high. I am starting to feel that the word…
Are Husby
  • 2,089
  • 2
  • 16
  • 14
39
votes
3 answers

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 column? Or just keep 2 columns for the tables I'm…
ab1428x
  • 794
  • 2
  • 8
  • 20
38
votes
3 answers

SQLAlchemy: filter by membership in at least one many-to-many related table

Using SQLAlchemy 0.7.1 and a MySQL 5.1 database, I've got a many-to-many relationship set up as follows: user_groups = Table('user_groups', Base.metadata, Column('user_id', String(128), ForeignKey('users.username')), Column('group_id',…
coredumperror
  • 8,471
  • 6
  • 33
  • 42
38
votes
3 answers

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.get(user=user) id_sala =…
Henry Ramos
  • 397
  • 1
  • 3
  • 7
38
votes
3 answers

Using EntityRepository::findBy() with Many-To-Many relations will lead to a E_NOTICE in Doctrine

For a Symfony2 project I had to create a relationship between a blog post and so called platforms. A platform defines a specific filter based on the domain you use to view the site. For example: If you join the site by url first-example.com, the…
devsheeep
  • 2,076
  • 1
  • 22
  • 31
38
votes
7 answers

Filter ManyToMany box in Django Admin

I have an object with a ManyToMany relation with another object. In the Django Admin this results in a very long list in a multiple select box. I'd like to filter the ManyToMany relation so I only fetch Categories that are available in the City that…
schmilblick
  • 1,917
  • 1
  • 18
  • 25
37
votes
6 answers

Should many to many tables have a primary key?

If I have two objects that have a many-to-many relationship, I would typically model them in my database schema with a many-to-many table to relate the two. But should that many-to-many table (or "join table") have a primary key of its own (integer…
ashes999
  • 9,925
  • 16
  • 73
  • 124
36
votes
4 answers

a new object was found through a relationship that was not marked cascade PERSIST

In trying to get a @OneToMany relationship between Article and HeaderField I probably have the mapping not quite right, resulting in: init: Deleting: /home/thufir/NetBeansProjects/USENET/build/built-jar.properties deps-jar: Updating property file:…
Thufir
  • 8,216
  • 28
  • 125
  • 273
35
votes
2 answers

How can I achieve a self-referencing many-to-many relationship on the SQLAlchemy ORM back referencing to the same attribute?

I'm trying to implement a self-referential many-to-many relationship using declarative on SQLAlchemy. The relationship represents friendship between two users. Online I've found (both in the documentation and Google) how to make a self-referential…
wocoburguesa
  • 738
  • 1
  • 5
  • 7
35
votes
1 answer

Add an object by id in a ManyToMany relation in Django

Django's ManyToMany field can be populated using my_field.add(my_instance), but as I understand it only my_instance.id is actually needed to perform the corresponding SQL query. If I want to add an object by its id, I can use…
pintoch
  • 2,293
  • 1
  • 18
  • 26
35
votes
5 answers

SQL many to many select

category_product --------------- id_category id_product product --------------- id_product id_manufacturer manufacturer --------------- id_manufacturer name How would I create an SQL query so that it selects all the names from manufacturer when…
Ken
  • 383
  • 1
  • 3
  • 4
35
votes
3 answers

How to use Rails 4 strong parameters with has_many :through association?

I'm having trouble getting a has_many :through association working with Rails 4's strong parameters. I have a model called Checkout and I need to select a person from the Employee model in the new checkout form. Checkouts and Employees are…
Lee McAlilly
  • 9,084
  • 12
  • 60
  • 94