many-to-many

[Rails] How to associate a new model with existing models using has_and_belongs_to_many

I have two models with a many to many relationship using has_and_belongs_to_many. Like so: class Competition < ActiveRecord::Base has_and_belongs_to_many :teams accepts_nested_attributes_for :teams end class Team < ActiveRecord::Base has_and_belongs_to_many :competitions accepts_nested_attributes_for :competitions end If we a...

how to save data in a many to many relationship using turbogears and sqlalchemy

hi i have a many to many relationship between a user and a group.and i will like to add a user with many groups in my database.how do i do that if my database is as follows user_group_table = Table('tg_user_group', metadata, Column('user_id', Integer, ForeignKey('tg_user.user_id', onupdate="CASCADE", ondelete="CASCADE")), ...

Using Entity Framework, how do I reflect a many to many relationship and add entites that exist to a new entity being created?

I'm new to Entity Framework and am trying to figure things out. I have a database created that's not very complicated. There's about 7 tables and 3 of those are mapping tables to associate one table record w/ another. The example I'm using here is this: Table User UserId UserName Table Role RoleId RoleName Table: UserRole UserI...

CodeIgniter Many-to-Many Relationship Management

Can anyone point out a good many-to-many database tutorial for CodeIgniter. Just trying to work out the process of creating, and then updating a many-to-many relationship. My example uses a multi-select of values, wondering how you take care of monitoring changes on update etc. ...

Many to Many Join Query in CodeIgniter

All previous attempts at a JOIN have left me with either the disc or item data populating the id, title keys of the result (maybe a clash is occuring). So I have: item table fields: id, title disc table fields: id, title item_disc table fields: item_id, disc_id How do I construct a double join so that I can access the related discs...

Hibernate bi-directional many-to-many cascade confusion

I'm a hibernate newbie and I'm not entirely sure how to get the cascade behavior I'm looking for. Let's say I have two classes A and B with bi-directional many-to-many mappings to each other. A is the owner side and B is the inverse side (I hope I have the terminology correct). public class A { private Set<B> bSet = new HashSet<B>(...

What is the best means to achieve efficient many-to-many scanning with Django models?

I have a many to many relationship in Django similar as this: class Account ( models.Model ): name = models.CharField( max_length = 30 ) user = models.ForeignKey( User, null = True, blank = True ) class Publisher ( models.Model ): publisherID = models.BigIntegerField( ) name = models.CharField( max_length = 30 ) lastRequested...

How to setup Hibernate @ManyToMany association with cascades on both foreign keys?

I'm trying to map a @ManyToMany association using hibernate. But so far I only managed to have cascade on one of the foreign keys. My source code goes like this: @Entity public class Airplane { @Id @GeneratedValue(strategy=GenerationType.AUTO) private Long id; @OnDelete(action=OnDeleteAction.CASCADE) @ManyToMany(m...

SQL count many-to-many values or have it counted every time new row is added?

I am using MySQL (MyISAM) 5.0.41 and I have this query: SELECT `x`.`items`.id, `x`.`items`.name, COUNT(*) AS count FROM `x`.`items` INNER JOIN `x`.`user_items` ON `x`.`items`.id = `x`.`user_items`.item_id GROUP BY name HAVING count > 2 ORDER BY count DESC I have about 36,000 users, 175,000 user_items and 60,000 items which...

looking for efficient way to group data on output in PHP

I have a table that holds products, another that holds variations and a third that hold product variations (many products can have many variations..both ways). I want to display (group by) each product and show the variations. what i've read is just looping thru and comparing id's to see which record you are on, if a new one then adjus...

How can I get a total count of a model's related objects and the model's children's related objects?

In Django, I've got a Checkout model, which is a ticket for somebody checking out equipment. I've also got an OrganizationalUnit model that the Checkout model relates to (via ForeignKey), as the person on the checkout belongs to an OrganizationalUnit on our campus. The OrganizationalUnit has a self relation, so several OUs can be the ch...

Query for a ManytoMany Field with Through in Django

Hi All, I have a models in Django that are something like this: class Classification(models.Model): name = models.CharField(choices=class_choices) ... class Activity(models.Model): name = models.CharField(max_length=300) fee = models.ManyToManyField(Classification, through='Fee') ... class Fee(models.Model): activity = m...

ActiveRecord::HasManyThroughAssociationNotFoundError in UserController#welcome

I have a many to many relationship in rails. All database tables are named accordingly and appropriately. All model files are plural and use underscore to seperate words. All naming comventions are followed by ruby and rails standards. I'm using has many through in my models like this: has_many :users, :through => :users_posts #Post...

LINQ, repository pattern and many-to-many relations

Hello, I've a problem with saving changes to database, I'm using LINQ2SQL mapping. I've implemented M:M relation (User <= UserRole => Role) based on tutorial: http://www.codeproject.com/KB/linq/linqtutorial2.aspx#premain25 Everything works fine when I'm using one class which is inherits from DataContext and is responsible for all of m...

Saving data from MultiSelectList in Edit and Create Views

I have 3 tables: Companies, Subcontracts, and CompanyToSubcontract The CompanyToSubcontract table is the guid of the Company and guid of the Subcontract. I have a MultiSelectList on the Subcontract Edit and Create views where the user can select multiple companies. I finally got it working where it displays the correct companies as se...

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

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(""...

Foreign keys not working in link table in Linq2Sql

I have 3 tables Subcontract, Company, and a link table CompanyToSubcontract. The link table contains the Subcontract_id and the Company_id. The foreign keys were set-up in SQL and when I drug them into my dbml the one-to-many relationship arrows showed up and everything looked fine. However, when coding, it's as if the relationship is...

SQL - many-to-many table primary key

This question comes up after reading a comment in this question: http://stackoverflow.com/questions/2190089/database-design/2190101 When you create a many-to-many table, should you create a composite primary key on the two foreign key columns, or create a auto-increment surrogate "ID" primary key, and just put indexes on your two FK co...

Help with LinqtoSql

Im using the Repository pattern and I want to write a method that receives a role and returns an Iqueryable of the users that belong to that role. (Im not sure if the right way would be to receive the role object or the role_id... in any case, how can I do this?? I dont like the query structure, I prefer the method structure of linq. use...

Django model form using forms.ModelMultipleChoiceField

Hi - I have a ModelForm in my Django app that uses a forms.ModelMultipleChoiceField, which displays as a forms.CheckboxSelectMultiple widget on the form. This ModelForm is used to select/de-select values for a many-to-many relation. Here's the problem: when you uncheck all of the checkboxes and save the form, it doesn't save. If you un...