many-to-many

SQL many to many select

爷,独闯天下 提交于 2019-11-30 01:34:54
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 id_category is equal to something? It's a straightforward inner join of the tables: SELECT m.name, cp.id_category FROM manufacturer as m INNER JOIN product as p ON m.id_manufacturer = p.id_manufacturer INNER JOIN category_product as cp ON p.id_product = cp.id_product WHERE cp.id_category = 'some value' Query without joins will look like following :

how to query many-to-many?

我怕爱的太早我们不能终老 提交于 2019-11-30 01:28:36
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 actors_movies actor_id int 11 movie_id int 11 How do I make a query to select movies that an actor is involved in? I am not asking for ruby on rails code. I want the actual mysql query string. Thank you! one thing to consider is that you are going to load the author object (because of RoR models), so with the ID would be enough: select movies.id, movies.name from movies inner

Doctrine 2 - Log changes in manyToMany relation

烂漫一生 提交于 2019-11-30 00:34:41
I use Loggable behavioral extension to log changes in my entities. I want to log changes in manyToMany relations too. I want to show to user this kind of change log: +--------------------------------------------------+ | Article "My Article" change log: | +-------+------------+-----------------------------+ | Who | When | What | +-------+------------+-----------------------------+ | Admin | 2015-07-01 | Removed tags "tag1", "tag2" | | Admin | 2015-07-01 | Added tags "tag3" | +-------+------------+-----------------------------+ Event problem I think, Doctrine doesn't fire events when manyToMany

stuck with asp.net mvc 3.0 Scaffolding in case of many to many relationship

こ雲淡風輕ζ 提交于 2019-11-29 23:35:46
问题 I am working on a mvc3.0 app using EF code first and mvc scaffolding. I am currently stuck with many to many relation between entities. I have following model. namespace BigApp.Models { #region POCO Objects public class Group { public int Id { get; set; } public string Name { get; set; } public DateTime CreatedOn { get; set; } public DateTime UpdatedOn { get; set; } public virtual ICollection<Project> Projects { get; set; } } public class Project { public int Id { get; set; } public string

Laravel - Pivot table for three models - how to insert related models?

混江龙づ霸主 提交于 2019-11-29 23:29:00
问题 I have three models with Many to Many relationships: User , Activity , Product . The tables look like id , name . And in the each model there are functions, for example, in User model: public function activities() { return $this->belongsToMany('Activity'); } public function products() { return $this->belongsToMany('Product'); } The pivot table User_activity_product is: id , user_id , activity_id , product_id . The goal is to get data like: User->activity->products . Is it possible to organize

Doctrine 2 : Best way to manage many-to-many associations

自作多情 提交于 2019-11-29 23:27:31
问题 Doctrine2 ORM have 2 technical ways to handle many-to-many associations : 1/ For a "simple" relation between 2 entities, and without additionnal attribute : Use @ManyToMany associations between entities In this case, the link table is used directly, without an association entity 2/ When link table introduces extra fields or more than 2 entities : Use an association class , ie an "real" entity to map the link table In this case, the direct ManyToMany association is replaced by OneToMany

How to update foreign key value in mysql database

南笙酒味 提交于 2019-11-29 23:19:09
I have three tables: categories, languages and categories_languages. Categories_languages is many to many table which links together categories and languages. I would like to update a foregin key value in table languages but it throws me error #1451 - Cannot delete or update a parent row: a foreign key constraint fails! CREATE TABLE IF NOT EXISTS `categories` ( `id` int(11) unsigned NOT NULL auto_increment, `name` varchar(20) NOT NULL, `modified` int(10) unsigned NOT NULL, PRIMARY KEY (`id`), ) ENGINE=InnoDB DEFAULT CHARSET=utf8; CREATE TABLE IF NOT EXISTS `languages` ( `id` char(2) NOT NULL,

How to count and display objects in relation ManyToMany in Django

拟墨画扇 提交于 2019-11-29 22:55:30
问题 I have a simple model with news and categories: class Category(models.Model): name = models.CharField() slug = models.SlugField() class News(models.Model): category = models.ManyToManyField(Category) title = models.CharField() slug = models.SlugField() text = models.TextField() date = models.DateTimeField() I want to count news for each category and display it on the website, like this: Sport (5) School (4) Films (6) Computer (2) etc... How can I do this?? Thanks! 回答1: Check out annotate()

Efficiently delete orphaned m2m objects/tags in Django

十年热恋 提交于 2019-11-29 21:45:22
问题 I have two models - Photo and Tag - which are connected via a ManyToManyField. class Photo(models.Model): tags = models.ManyToManyField(Tag) class Tag(models.Model): lang = models.CharField(max_length=2) name_es = models.CharField(max_length=40) name_en = models.CharField(max_length=40) Every once in a while, we get orphaned tags, that are not referenced any more by any photo. Is there an efficient way of deleting those tags? I know about this answer: Django: delete M2M orphan entries? And

@ManyToMany(mappedBy = “foo”)

落爺英雄遲暮 提交于 2019-11-29 21:21:42
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-directional relationship , other than whether table is called foo_bar, or bar_foo; and without the mappedBy attribute I get two join tables, both foo_bar and bar_foo. JB Nizet The documentation says: If the association is bidirectional, one side has to be the owner and one side has to be the inverse end (ie. it will be ignored when updating the relationship values in the association table): So, the side which has the