many-to-many

Many-to-many declarative SQLAlchemy definition for users, groups, and roles

让人想犯罪 __ 提交于 2019-12-03 13:36:14
问题 I'm new to SQLAlchemy, and was wondering what the best way to go about defining such tables and a relationship would be. I want to be able to access groups from a user by user.groups , users in a group by group.users , and find out the role of a user in a group (which I'm assuming will be logic defined in an association model). I also want to select all users, group by group, and include role titles. I've tried using the tutorial to create the association table (declarative style) and

Django: how to aggregate / annotate over a many-to-many relationship?

久未见 提交于 2019-12-03 13:12:06
I have a Person model and a Tag model, with a m2m between them. I need to extract the tag which is connected to the most records within a given Person queryset, together with the count. Is there an elegant, efficient way to extract this using the Django ORM? Better yet, is there a way to get the entire tag distribution through some annotation? How can one even pull all of the objects connected to a subset of objects connected via a m2m? Thanks! GJ. This would give you the most frequent tag: from django.db.models import Count Tag.objects.filter(person__yourcriterahere=whatever [, morecriteria])

How to map an ArrayList of primitives to a single column?

时间秒杀一切 提交于 2019-12-03 12:42:02
Let's say I have the following situation: Object Car has an ArrayList of prices, which are all numbers. Is it possible in Hibernate to save all the prices in a single column? I know this violates the first normal form but there might be cases when you don't want them to be saved in a separate table like it's classically done in One-To-Many or Many-To-Many relationships. In JDO I'd do this easily by saving the ArrayList in a BLOB column. Some useful related SOF questions: ArrayList of primitive types in Hibernate and Map ArrayList with Hibernate . Any idea will be highly appreciated! Chris

Deletions in a many-to-many structure

不羁岁月 提交于 2019-12-03 10:47:14
I just want to check really quickly. Say I have two entities in a data model: Catalog, and Product. They have a many-to-many relationship with each other, and both are required (a Catalog must have at least one Product, and all Products must each belong to at least one Catalog). So if I was to delete a Product, its deletion should be Nullify, of course. But what should the deletion policy be for Catalog? If a Catalog is deleted, not all of its Products necessarily exclusively belong to it. A Product may belong to more than one Catalog. So I definitely shouldn't use Cascade. However, is Nullify

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

99封情书 提交于 2019-12-03 10:13:41
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 to a single key. public partial class M2M2ParentAMap : ClassMap<M2M2ParentA> { public M2M2ParentAMap()

How to query directly the table created by Django for a ManyToMany relation?

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-03 09:11:27
问题 I have a model MyModel2 with a ManyToManyField related to another model MyModel1 . How can I get the pairs mymodel1.id, mymodel2.id , as represented in the table Django create for this relation? Do I have to do a raw SQL query on this table or is it possible through the object managers of this models? class MyModel1(models.Model): name = models.CharField(max_length=50) class MyModel2(models.Model): name = models.CharField(max_length=50) mymodel1 = models.ManyToManyField(MyModel1) 回答1: This is

EF 4.1 RC Many to many relationship in EF CF

a 夏天 提交于 2019-12-03 09:07:33
I have two entities with many to many relationship like this: class author { public int AuthorID{get;set;} public string Name{get;set;} public virtual ICollection<book> books{get;set;} } class book { public int BookID{get;set;} public string Name{get;set;} public virtual ICollection<author> authors{get;set;} } and I have an intermediate table named Bookauthor defined like this: BookAuthor: int int AuthorID int BookID How to map this using FluentAPI Thanks!! This was problematic with EDMX but with EF 4.1 fluent API you can map it : modelBuilder.Entity<book>() .HasMany(b => b.authors) .WithMany

CRUD Views For Many-Many Relationship, Checkboxes

跟風遠走 提交于 2019-12-03 08:37:29
I am having a hard time trying to figure out what I need to do to get this to work. I'm learning ASP.NET MVC CodeFirst with EF. If I make a model I can simply add a controller for that model and add scaffolding to create views that automatically take care of CRUD. But now I have two models, Project and Category. They have a many to many relationship and database is designed correctly with the associative table without having to make a separate model for it. The code for the models is this.... public class Project { public int ProjectId { get; set; } public string Title { get; set; } public

Django, set initial data to formset with ManyToMany

*爱你&永不变心* 提交于 2019-12-03 08:26:54
I need to set initial data to formset with ManyToMany field. Usually i'm doing like this when there is no ManyToMany field in forms of my fomset: PersonFormSet = forms.formsets.formset_factory(NickName, can_delete=True) init_data = [{'name':'Vasya pupkin','nick':'Vasya'}, {'name':'Vasyapupkin','nick':'Petya'}] nick_formset = PersonFormSet(initial=init_data) But now I need to set ManyToMany field initial data and tried something like this: NickNameFormSet = forms.formsets.formset_factory(NickName, can_delete=True) init_data = [{'name': 'Vasya Pupkin', 'nick': {'Vasya':'selected', 'Petya':

JPA Annotations for many-to-many relation between objects of the same entity

只愿长相守 提交于 2019-12-03 08:25:25
I want to implement a Role Hierarchy but am rather new to JPA Annotations. I have a Role Entity with a name and an id(implicit via AbstractPersistable ): @Entity @Table(name="role") public class Role extends AbstractPersistable<Long> { private static final long serialVersionUID = 8127092070228048914L; private String name; Now I want to be able to define the following relationships: a Role can have many child roles a Role can be child to many roles How would I do that with Hibernate annotations? Can I define this inside the Role Entity @ManyToMany(cascade = CascadeType.MERGE) @JoinTable( name =