many-to-many

JPA: which side should be the owning side in a m:n relationship?

岁酱吖の 提交于 2019-11-30 08:59:00
Say, for example, I had two entities: Article and Tag (like in a typical blog). Each article can have many tags, and each tag can be used by many articles, so it is a classical m:n relationship. I need to specify an owning side with JPA. But which side should be the owning side? An article doesn't depend on a certain tag and vice versa. Is there a rule of thumb for determining which side should be the owning side? Every bidirectional relationship requires an owning side in JPA. In the particular case of ManyToMany : @JoinTable is specified on the owning side of the relationship. the owning

Populating a SQLAlchemy many-to-many relationship using ID's instead of objects

有些话、适合烂在心里 提交于 2019-11-30 08:52:26
The situation: So, I have a basic many-to-many relationship in SQLAlchemy using an association table . For example, a person can attend many parties, and a party can have many persons as guests: class Person(Base): __tablename__ = 'person' id = Column(Integer, primary_key=True) name = db.Column(db.String(50)) class SexyParty(Base): __tablename__ = 'sexy_party' id = Column(Integer, primary_key=True) guests = relationship('Person', secondary='guest_association', lazy='dynamic', backref='parties') guest_association = Table( 'guest_association', Column('user_id', Integer(), ForeignKey('person.id')

Symfony2 Doctrine2 Many To Many Form not Saving Entities

本小妞迷上赌 提交于 2019-11-30 08:22:14
I am having some trouble with a many to many relationship. I have Users and Assets . I would like to be able to assign users to an asset on the asset page. The code below displays a list of users when creating/editing an asset, however changes made to the user checkboxes do not save, while the rest of the data is persisted. If I add an entry to users_assets through the mysql client, these changes are shown in the asset list. User class User extends BaseUser { /** * @ORM\ManyToMany(targetEntity="Asset", inversedBy="users") */ private $assets; } Asset class Asset { /** * @ORM\ManyToMany

Many to many (join table) relationship with the same entity with codefirst or fluent API?

ε祈祈猫儿з 提交于 2019-11-30 07:38:51
问题 I am developing with EF 4.3.1 CodeFirst. I have an Airport table as shown below: public class Airport { [Key] public int ID { get; set; } public string Name{ get; set; } } What I need is a Route table with 2 FKs from the same Airport table like: public class Route { public int DepartureAirportID { get; set; } public int DestinationAirportID { get; set; } public virtual Airport DestinationAirport { get; set; } public virtual Airport DepartureAirport { get; set; } } How can this be achieved?

Insert operation with many-to-many relationship using EF

。_饼干妹妹 提交于 2019-11-30 07:18:13
I've two model classes: public class Candidate { public int Id { get; set; } public string Name { get; set; } public ICollection<Job> Jobs { get; set; } } public class Job { public int Id { get; set; } public string Name { get; set; } public ICollection<Candidate> Candidates { get; set; } } My DbContext name is JobsContext. The above code generates me 3 tables Candidates, Jobs & CandidatesJobs(autogenerated by EF) Now I've records in Jobs table : Id = 1, Name = "Sales" : Id = 2, Name = "Engineer". I want to associate a new Candidate which I'll be inserting into Candidates table with the 2

Many-to-many data structure in Python

风流意气都作罢 提交于 2019-11-30 06:59:31
问题 I have a data set of books and authors, with a many-to-many relationship. There are about 10^6 books and 10^5 authors, with an average of 10 authors per book. I need to perform a series of operations on the data set, such as counting the number of books by each author or deleting all books by a certain author from the set. What would be a good data structure that will allow fast handling? I'm hoping for some ready made module that can provide methods along the lines of: obj.books.add(book1) #

SQLAlchemy: filter by membership in at least one many-to-many related table

戏子无情 提交于 2019-11-30 06:31:22
问题 Using SQLAlchemy 0.7.1 and a MySQL 5.1 database, I've got a many-to-many relationship set up as follows: user_groups = Table('user_groups', Base.metadata, Column('user_id', String(128), ForeignKey('users.username')), Column('group_id', Integer, ForeignKey('groups.id')) ) class ZKUser(Base, ZKTableAudit): __tablename__ = 'users' username = Column(String(128), primary_key=True) first_name = Column(String(512)) last_name = Column(String(512)) groups = relationship(ZKGroup, secondary=user_groups,

Prevent duplicates in the database in a many-to-many relationship

假装没事ソ 提交于 2019-11-30 05:22:07
I'm working on a back office of a restaurant's website. When I add a dish , I can add ingredients in two ways. In my form template, I manually added a text input field. I applied on this field the autocomplete method of jQuery UI that allows: Select existing ingredients (previously added) Add new ingredients However, when I submit the form, each ingredients are inserted in the database (normal behaviour you will tell me ). For the ingredients that do not exist it is good, but I don't want to insert again the ingredients already inserted. Then I thought about Doctrine events , like prePersist()

How do you do many to many table outer joins?

好久不见. 提交于 2019-11-30 05:11:47
I have 3 tables, foo, foo2bar, and bar. foo2bar is a many to many map between foo and bar. Here are the contents. select * from foo +------+ | fid | +------+ | 1 | | 2 | | 3 | | 4 | +------+ select * from foo2bar +------+------+ | fid | bid | +------+------+ | 1 | 1 | | 1 | 2 | | 2 | 1 | | 2 | 3 | | 4 | 4 | +------+------+ select * from bar +------+-------+------+ | bid | value | zid | +------+-------+------+ | 1 | 2 | 10 | | 2 | 4 | 20 | | 3 | 8 | 30 | | 4 | 42 | 30 | +------+-------+------+ What I want to request is, "Give me a list of all the fid and values with zid of 30" I expect an

Query examples in a many-to-many relationship

拟墨画扇 提交于 2019-11-30 05:09:27
Wow, it's hard to find a simple explanation to this topic. A simple many-to-many relationship. Three tables, tableA, tableB and a junction tableA_B. I know how to set up the relationship, with keys and all, but I get a little confused when time comes to perform INSERT, UPDATE and DELETE queries.... Basically, what I am looking for is an example that shows: How to get all records in TableA, based on an ID in TableB How to get all records in TableB, based on an ID in TableA 3 How to INSERT in either TableA or TableB, and then make the appropriate INSERT in the junction table to make the