Django’s ManyToMany Relationship with Additional Fields

Here is example of what you want to achieve: http://docs.djangoproject.com/en/dev/topics/db/models/#extra-fields-on-many-to-many-relationships In case link ever breaks: from django.db import models class Person(models.Model): name = models.CharField(max_length=128) def __str__(self): # __unicode__ on Python 2 return self.name class Group(models.Model): name = models.CharField(max_length=128) members = models.ManyToManyField(Person, through=”Membership”) def __str__(self): # __unicode__ on Python 2 return self.name class Membership(models.Model): person = … Read more

SQLAlchemy ManyToMany secondary table with additional fields

You will have to switch from using a plain, many-to-many relationship to using an “Association Object”, which is basically just taking the association table and giving it a proper class mapping. You’ll then define one-to-many relationships to User and Community: class Membership(db.Model): __tablename__ = ‘community_members’ id = db.Column(‘id’, db.Integer, primary_key=True) user_id = db.Column(db.Integer, db.ForeignKey(‘user.id’)) community_id … Read more

Saving Many to Many relationship data on MVC Create view

Edit: I’ve written this up in 3 blog posts with code part 1 sets up the solution and creates a new user part 2 adds the courses and saves them with the user profile part 3 allows editing and deletion of users and their courses Github source: https://github.com/cbruen1/mvc4-many-to-many I think you’ve strayed from conventions a … Read more

Rails find_or_create_by more than one attribute?

Multiple attributes can be connected with an and: GroupMember.find_or_create_by_member_id_and_group_id(4, 7) (use find_or_initialize_by if you don’t want to save the record right away) Edit: The above method is deprecated in Rails 4. The new way to do it will be: GroupMember.where(:member_id => 4, :group_id => 7).first_or_create and GroupMember.where(:member_id => 4, :group_id => 7).first_or_initialize Edit 2: Not … Read more

Difference Between One-to-Many, Many-to-One and Many-to-Many?

Looks like everyone is answering One-to-many vs. Many-to-many: The difference between One-to-many, Many-to-one and Many-to-Many is: One-to-many vs Many-to-one is a matter of perspective. Unidirectional vs Bidirectional will not affect the mapping but will make difference on how you can access your data. In Many-to-one the many side will keep reference of the one side. … Read more

Many-to-many mapping table

Do this on your DbContext OnModelCreating: protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Entity<Recipe>() .HasMany(x => x.Members) .WithMany(x => x.Recipes) .Map(x => { x.ToTable(“Cookbooks”); // third table is named Cookbooks x.MapLeftKey(“RecipeId”); x.MapRightKey(“MemberId”); }); } You can do it the other way around too, it’s the same, just another side of the same coin: modelBuilder.Entity<Member>() .HasMany(x => … Read more

SQL – many-to-many table primary key

With a simple two-column many-to-many mapping, I see no real advantage to having a surrogate key. Having a primary key on (col1,col2) is guaranteed unique (assuming your col1 and col2 values in the referenced tables are unique) and a separate index on (col2,col1) will catch those cases where the opposite order would execute faster. The … Read more

MongoDB Many-to-Many Association

Depending on your query needs you can put everything in the user document: {name:”Joe” ,roles:[“Admin”,”User”,”Engineer”] } To get all the Engineers, use: db.things.find( { roles : “Engineer” } ); If you want to maintain the roles in separate documents then you can include the document’s _id in the roles array instead of the name: {name:”Joe” … Read more