many-to-many

Django, in many to many relationship within the self class, how do I reference each other in terms of ORM?

北慕城南 提交于 2020-01-13 18:02:04
问题 class User(models.Model): name = models.CharField(max_length=100) age = models.IntegerField() gender = models.IntegerField() email = models.CharField(max_length=100) password = models.CharField(max_length=255) following = models.ManyToManyField("self", related_name='followers') objects = UserManager() def __repr__(self): return "User: {0}".format(self.name) In my model, User, users can follow and followed by each other. I can find who the user is following by this: user1 = User.objects.get(id

Save M2M “Through” Inlines in Django Admin

限于喜欢 提交于 2020-01-13 10:28:09
问题 Apparently Django's ModelAdmin/ModelForm doesn't allow you to use save_m2m() if there's an intermediate through table for a ManyToManyField. models.py: from django.db import models def make_uuid(): import uuid return uuid.uuid4().hex class MyModel(models.Model): id = models.CharField(default=make_uuid, max_length=32, primary_key=True) title = models.CharField(max_length=32) many = models.ManyToManyField("RelatedModel", through="RelatedToMyModel") def save(self, *args, **kwargs): if not self

How to delete a row in join table with JPA

江枫思渺然 提交于 2020-01-13 10:12:09
问题 I have the model below : an article can have some tags and a tag can be on some articles. So it is a many-to-many relationship with 3 tables : ARTICLE ARTICLE_TAG TAG When I delete a tag, I want to delete : the tag in TAG all relations between the tag and the articles tagged in ARTICLE_TAG But I don't want to delete the articles in ARTICLE of course. How can I do that ? I try this, but it doesn't work : Session session = HibernateUtil.getSessionFactory().getCurrentSession(); for (Article

Is it better to use INNER JOIN or EXISTS to find belonging to several in m2m relation?

柔情痞子 提交于 2020-01-12 12:29:47
问题 Given m2m relation: items-categories I have three tables: items , categories and items_categories that hold references to both I want to find an item belonging to all given category sets: Find Item belonging to a category in [1,3,6] and belonging to a category in [7,8,4] and belonging to a category in [12,66,42] and ... There are two ways I can think of to accomplish this in mySQL. OPTION A: INNER JOIN: SELECT id from items INNER JOIN category c1 ON (item.id = c1.item_id) INNER JOIN category

Is it better to use INNER JOIN or EXISTS to find belonging to several in m2m relation?

▼魔方 西西 提交于 2020-01-12 12:27:32
问题 Given m2m relation: items-categories I have three tables: items , categories and items_categories that hold references to both I want to find an item belonging to all given category sets: Find Item belonging to a category in [1,3,6] and belonging to a category in [7,8,4] and belonging to a category in [12,66,42] and ... There are two ways I can think of to accomplish this in mySQL. OPTION A: INNER JOIN: SELECT id from items INNER JOIN category c1 ON (item.id = c1.item_id) INNER JOIN category

EF Code First with many to many self referencing relationship

烈酒焚心 提交于 2020-01-12 04:57:30
问题 I am starting out with using the EF Code First with MVC and am a bit stumped with something. I have the following db structure (Sorry but I was not allowed to post an image unfortunately): Table - Products Table - RelatedProducts 1-Many on Products.ProductID -> RelatedProducts.ProductID 1-Many on Products.ProductID -> RelatedProducts.RelatedProductID Basically I have a product that can have a series of products that are related to it. These are kept in the RelatedProducts table with the

mysql n:m relationship: Find rows with several specific relations

自作多情 提交于 2020-01-11 14:22:14
问题 I have two SQL Tables, 'products' and 'tags'. They have an n:m relationship, using a third table 'product_tags'. I want to use a query to find every product that has a number of specific tags. For example, find every products that has a relation to the tags 1, 23 and 54. Is there a way to do this with just one query? 回答1: You can use this solution. This gets all products that contain ALL keywords 1, 23, and 54: SELECT a.* FROM products a INNER JOIN product_tags b ON a.product_id = b.product

Recursive CTE - Get descendants (many-to-many relationship)

删除回忆录丶 提交于 2020-01-11 11:21:52
问题 What I have: Given a tree (or more like a directed graph) that describes how a system is composed by its generic parts. For now let this system be e.g. the human body and the nodes its body parts. So for instance 3 could be the liver that has a left and a right lobe ( 6 and 9 ), in both of which there are veins ( 8 ) (that can also be found at any unspecified place of the liver, hence 8 -> 3 ) but also in the tongue ( 5 ). The lung ( 7 ) - which is in the chest ( 4 ) - also has a right lobe,

Select rows with multiple tags… is there a better way?

醉酒当歌 提交于 2020-01-11 10:33:09
问题 I am attempting to do a tag system for selecting products from a database. I have read that the best way to achieve this is via a many-to-many relationship as using LIKE '%tag%' is going to get slow when there are a lot of records. I have also read this question where to match on multiple tags you have to do a join for each tag being requested. I have 3 tables: shop_products, shop_categories and shop_products_categories. And I need to, for example, be able to find products that have both the

@ManyToMany relation not save

假如想象 提交于 2020-01-11 06:33:27
问题 I have some entities with @ManyToMany relation: @ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER) @JoinTable(name = "buses_drivers", joinColumns = @JoinColumn (name = "driver_id_inner", referencedColumnName = "driver_id"), inverseJoinColumns = @JoinColumn (name = "bus_id_inner", referencedColumnName = "bus_id")) private List<Bus> buses; and @ManyToMany(mappedBy = "buses", fetch = FetchType.EAGER, cascade = CascadeType.ALL) private List<Driver> drivers; When execute saving Driver