many-to-many

Best way to handle Many-to-Many relationships in PHP MySQL

浪尽此生 提交于 2019-12-04 19:19:13
I am looking for the best way to handle a database of many-to-many relationships in PHP and MySQL. Right now I have 2 tables: Users (id, user_name, first_name, last_name) Connections (id_1, id_2) In the User table id is auto incremented on add and user_name is unique, but can be changed. Unfortunately, I don't have control over the user_name and its ability to be changed, but I must account for it. The Connections table is obviously, user1 and user2's id. The connection table needs to account for these possible relations: user1 --> user2 (user 1 friends with user 2 but not user2 friends with

Laravel Eloquent Many-to-Many query whereIn

风格不统一 提交于 2019-12-04 18:21:41
问题 In my application I have updated a relationship from one-to-many to many-to-many and I'm trying to figure out a way to persist associated functionality. Let's say I have two related tables, e.g. dogs and owners. If I have an array of owners and I'm trying to get a list of dogs id's for those owners, how should I do it eloquently? Similar question was asked here: https://laracasts.com/discuss/channels/laravel/getting-many-to-many-related-data-for-an-array-of-elements So, How would I get the

Many to Many: Delete one side, the relationship entry BUT don't delete the other side

╄→гoц情女王★ 提交于 2019-12-04 17:10:21
问题 I want to delete an user which has many usergroups but those usergroups don't belong exclusivly to this user: other users can also be using this usergroups. And usergroups can exist even if no user references them. I want to map the many-to-many relationship so that if a user is deleted , the relationship is automatically deleted but NOT the usergroup ? I tried Cascade.All as I thought cascades on many-to-many affect the relationship but not the other side. I thought that only Cascade

Ordering First Model in Association with Second Model

守給你的承諾、 提交于 2019-12-04 16:55:12
I'm working on my first project with RoR and I need to create many to many relationship between two models but with possibility of ordering objects of first model in association to second model. Let's say that I have two following models - Customer - Route I want assign many Customers to many Routes but with storing order of this association, so for example -Route 1 --Customer 2, position 1 --Customer 1, position 2 -Route 2 --Customer 3, position 1 --Customer 1, position 2 I think I have to use for it has_many :through and belong_to and creat "position" field in in-middle-table but then how to

Django: removing item from many-to-many relation more efficiently

吃可爱长大的小学妹 提交于 2019-12-04 16:07:07
My book class uses a many-to-many field to save its readers. If I want to remove a reader from some books, I can use a loop to go through all book objects to remove the reader. But it's too slow. Is it possible to do it in a bulk operation? class Book(models.Model): readers = models.ManyToManyField(User, related_name='books') #Remove reader 'foo' from book 1, 2, 3, 4, 5. However, it is slow. for book in Book.objects.filter(id__in=[1, 2, 3, 4, 5]) book.readers.remove(R) Yes. You can access the underlying M2M model using the through attribute , and then use that model to do a delete in one query

Many-to-Many with ECTO and put_assoc/4

送分小仙女□ 提交于 2019-12-04 15:46:15
I try associate 2 existing Many-to-Many records with ECTO and put_assoc/4 but won't remove elements when try update. Basically i have projects and users . for manage the access of users to projects i have the table "user_project". def Project do schema "project" do ... # users (if user_type is :admin) many_to_many( :users, User, join_through: "user_project" ) ... end end def User do schema "user" do ... # users (if user_type is :admin) many_to_many( :projects, User, join_through: "user_project" ) ... end ... def changeset_insert_not_active(%User{} = user, attrs) do user |> cast(attrs,

How do you create a model with categories that has subcategories (using Rails)?

泪湿孤枕 提交于 2019-12-04 15:22:48
I am creating a blog through Rails. I am relating the posts and categories models through a many-to-many relationship. How do I incorporate subcategories in this model? Hierarchy The best way is to use one of the hierarchy gems, typically Ancestry or Closure_Tree to create a "tree" type structure for your categories . You'll then be able to associate blogs with each category, creating the hierarchy functionality you need. We've achieved this before - using the Ancestry gem: Categories As you can see from the above image, the "secret sauce" you're missing is that your categories are not in a

Grails: Many-to-Many without hasMany/belongsTo - instead using native 3NF - Searching full text

南楼画角 提交于 2019-12-04 15:13:55
I am implementing a many-to-many mapping in grails using 3NF, Not using the hasMany or belongsTo property. Taken from this article it shows and explains quite a lot about its advantages. Article: http://burtbeckwith.com/blog/?p=169 Presentation notes: http://burtbeckwith.com/blog/files/169/gorm%20grails%20meetup%20presentation.pdf I'm trying to make a Tag system onto questions, kind of like this(stackoverflow :)) I can save the Question and the Tags, then save the association with them, but now I want to be able to search and serve up a full Question with Tags, I have 3 domain classes -

EF Core 2.0 migration - Many-to-Many with additional fields

时间秒杀一切 提交于 2019-12-04 15:12:23
I'm using EF Core 2.0 and created a many-to-many relationship with a join entity. When I add a new migration EF always creates an additional Index/Id-field which is completely stupid. Here is my join entity: public class Team_Member { public int TeamId { get; set; } public Team Team { get; set; } public int MemberId { get; set; } public Member Member { get; set; } public MemberTypeEnum MemberType { get; set; } } This is the configuration of the join table (following several examples on the internet): public class Team_MemberConfig : IEntityTypeConfiguration<Team_Member> { public void Configure

Fluent NHibernate: How to Map M:N many-to-many with composite keys on both sides

孤街浪徒 提交于 2019-12-04 14:36:58
问题 OK, so here is the problem. Its not even as crazy as the guy who wants to map m:n with different column counts in his PKs. No matter what I do or where I look there seems to be no method chain that will result in a successful mapping of this. I have tried doubling up on the Parent and Child columns, eg ParentColumn("").ParentColumn("").ChildColumn("").ChildColumn("") - didnt' think it would work and I was right. Tried just using ForeignKeyConstraintNames no luck. Still FNH is mapping one side