many-to-many

Laravel many to many loading related models with count

て烟熏妆下的殇ゞ 提交于 2019-11-27 02:27:12
问题 I am trying to link 4 tables and also add a custom field calculated by counting the ids of some related tables using laravel. I have this in SQL which does what I want, but I think it can be made more efficient: DB::select('SELECT posts.*, users.id AS users_id, users.email,users.username, GROUP_CONCAT(tags.tag ORDER BY posts_tags.id) AS tags, COUNT(DISTINCT comments.id) AS NumComments, COUNT(DISTINCT vote.id) AS NumVotes FROM posts LEFT JOIN comments ON comments.posts_id = posts.id LEFT JOIN

Class-based views for M2M relationship with intermediate model

怎甘沉沦 提交于 2019-11-27 02:26:13
问题 I have a M2M relationship between two Models which uses an intermediate model. For the sake of discussion, let's use the example from the manual: class Person(models.Model): name = models.CharField(max_length=128) def __unicode__(self): return self.name class Group(models.Model): name = models.CharField(max_length=128) members = models.ManyToManyField(Person, through='Membership') def __unicode__(self): return self.name class Membership(models.Model): person = models.ForeignKey(Person) group

Many-to-many on the same table with additional columns

微笑、不失礼 提交于 2019-11-27 01:40:24
问题 I have a class User. A user can be a friend with many other users. The relationship is mutual. If A is a friend of B then B is a friend of A. Also I want every relation to store additional data - for example the date when two users became friends. So this is a many-to-many relationship on the same table with additional columns. I know that a middle class Friendship should be created(containing two user ids and column for the date). But I am coming short at mapping this with Hibernate. The

SQLite many-to-many relationship?

醉酒当歌 提交于 2019-11-27 01:36:32
问题 I'm trying to set up a SQLite3 database with foo s and bar s and a many-to-many relation between them. This is what I've got so far: CREATE TABLE foo( id INTEGER PRIMARY KEY NOT NULL, foo_col INTEGER NOT NULL ); CREATE TABLE bar( id INTEGER PRIMARY KEY NOT NULL, bar_col TEXT NOT NULL ); CREATE TABLE foobar( foo_id INTEGER, bar_id INTEGER, FOREIGN KEY(foo_id) REFERENCES foo(id) ON DELETE CASCADE, FOREIGN KEY(bar_id) REFERENCES bar(id) ON DELETE CASCADE ); CREATE INDEX fooindex ON foobar(foo_id

how to make a composite primary key (java persistence annotation)

你。 提交于 2019-11-27 01:27:56
How to make it so that the table user_roles defines the two columns (userID, roleID) as a composite primary key. should be easy, just can't remember/find. In user entity: @ManyToMany(fetch = FetchType.LAZY) @JoinTable(name = "user_roles") public List<RoleDAO> getRoles() { return roles; } @Id @GeneratedValue(strategy = GenerationType.AUTO) public Integer getUserID() { return userID; } In roles entity: @ManyToMany(fetch = FetchType.LAZY) @JoinTable(name = "user_roles") public List<UserDAO> getUsers() { return users; } @Id @GeneratedValue(strategy = GenerationType.AUTO) public Integer getRoleID()

Entity framework and many to many queries unusable?

拥有回忆 提交于 2019-11-27 01:02:19
问题 I'm trying EF out and I do a lot of filtering based on many to many relationships. For instance I have persons, locations and a personlocation table to link the two. I also have a role and personrole table. EDIT: Tables: Person (personid, name) Personlocation (personid, locationid) Location (locationid, description) Personrole (personid, roleid) Role (roleid, description) EF will give me persons, roles and location entities. EDIT: Since EF will NOT generate the personlocation and personrole

Doctrine 2: How to handle join tables with extra columns

六月ゝ 毕业季﹏ 提交于 2019-11-27 00:37:40
问题 How do I setup a join table with extra columns, or a many-to-many association with additional properties, in Doctrine 2? 回答1: First off, let me explain that this does not exist: A join table (also known as a junction table or cross-reference table ) is a table that links 2 (or more) other tables together within the same database by primary key. This means that a join table will only contain foreign keys, there is no place for these extra columns. So when you need extra columns in such a table

Hibernate many-to-many cascading delete

老子叫甜甜 提交于 2019-11-27 00:37:25
问题 I have 3 tables in my database: Students , Courses and Students_Courses Students can have multiple courses and courses can have multiple students. There is a many-to-many relationship between Students and Courses . I have 3 cases for my project and courses added to my Courses table. (a) When I add a user, it gets saved fine, (b) When I add courses for the student, it creates new rows in User_Courses - again, expected behaviour. (c) When I am trying to delete the student, it is deleting the

Filter ManyToMany box in Django Admin

半腔热情 提交于 2019-11-26 23:49:19
问题 I have a object with a many-to-many relation with another object. In the Django Admin this results in a very long list in a multiple select box. I'd like to filter the ManyToMany relation so I only fetch Categories that is available in the City that the Customer have selected. Is this possible? Will I have to create a widget for it? And if so - how do I copy the behavior from the standard ManyToMany field to it, since I would like the filter_horizontal function as well. These are my

Am I doing many to many incorrectly when using fluent nhibernate?

只谈情不闲聊 提交于 2019-11-26 22:12:55
问题 I have two main entities (db tables) Project Application I have a bridge tabled called ProjectApplication with 3 col (Id, ProjectId, ApplicationId) A project can have many applications. An application can below to many different project Your basic many to many mapping Currently this is what i have setup in my fluent nhibernate mapping files public class ProjectMap { HasMany(x => x.ProjectApplications) .AsBag().Inverse().Cascade.AllDeleteOrphan().Fetch.Select().BatchSize(80); } public class