many-to-many

Database theory - relationship between two tables

岁酱吖の 提交于 2019-12-12 13:37:23
问题 I have a database with two tables - let's call them Foo and Bar. Each foo may be related to any number of bars, and each bar may be related to any number of foos. I want to be able to retrieve, with one query, the foos that are associated with a certain bar, and the bars that are associated with a certain foo. My question is, what is the best way of recording these relationships? Should I have a separate table with records of each relationship (e.g. two columns, foo and bar)? Should the foo

How to .update m2m field in django

痞子三分冷 提交于 2019-12-12 12:16:30
问题 I have: class MyUser(Model): today_ref_viewed_ips = ManyToManyField( UniqAddress, related_name='today_viewed_users', verbose_name="Adresses visited referal link today") ... On some croned daily request I do: for u in MyUser.objects.all(): u.today_ref_viewed_ips.clear() Can it be done on DB server with update? MyUser.objects.all().update(...) Ok, I can't update, thanks. But only thing I need is to TRUNCATE m2m internal table, is it possible to perform from django? How to know it's name

Multiple relationships with single mapping table without generating foreign keys by Hibernate

╄→尐↘猪︶ㄣ 提交于 2019-12-12 10:18:02
问题 I have two base abstract classes and there are multiple additional classes derived from these two, adding additional attributes etc. There exist relations between those specific derived types. A simple illustrating example: Role and Group classes are abstract , but they are not marked as @MappedSuperclass . InheritanceType.JOINED strategy is being used, so both tables Role (for abstract class) and AdminRole (for derived class) should exist (they both will have the same RoleID ).

JPA @manytomany unidirectional mapping

给你一囗甜甜゛ 提交于 2019-12-12 10:15:12
问题 When I try remove ExternalDataStorage This query throw a constraint violation exception with constraint name null .ExternalDataStorage has no relation mapping on it.Query first check ExternalTasks whichs contain ExternalDataStorage(Which will be removed) in ExternalTask's externalsources List and remove from list, if list is empty remove externaltask and finally remove ExternalDataStorage entity(Target entity). public boolean deleteExternalDataStorage(Long sid) { EntityManager em =

Add an object by id in a ManyToMany relation in Django

早过忘川 提交于 2019-12-12 09:30:08
问题 Django's ManyToMany field can be populated using my_field.add(my_instance) , but as I understand it only my_instance.id is actually needed to perform the corresponding SQL query. If I want to add an object by its id, I can use my_field.add(MyModel.objects.get(id=id)) , but this will generate two queries instead of one. How can I avoid this extra query? 回答1: Although the documentation of the add method does not mention it, you can actually pass directly an id to it, like this: my_instance.add

entity framework 4 many to many update

安稳与你 提交于 2019-12-12 08:14:39
问题 I have 3 tables - User (Id, Name) Roles (Id, Name) UserRoles (UserId, RoleId) I think they are self explanatory. How do I update an entry (UserId and RoleId) in UserRoles? context.User.Roles gives me the list of roles but how do I update them? Thank you. 回答1: From your comment: context.User.Roles gives me the list of roles. I can do a for-each and update the Id, but how do I update the corresponding UserId foreach RoleId in that table? First of all, you should NOT update the Id's. Secondly,

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

社会主义新天地 提交于 2019-12-12 07:15:50
问题 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

Rails has-many-through equivalent in ASP.NET MVC3

情到浓时终转凉″ 提交于 2019-12-12 07:07:27
问题 In .NET Entity Framework, what is the best way to have a (custom) join table with extra attributes (other than ids) and/or associate this join table with others via separate model? In Ruby on Rails we can have a model for the join table, like: Item.rb (model) :has_many => :buyers, :through=>:invoice ... Buyers.rb (model) :has_many => :items, :through=>:invoice ... Invoice.rb (model) :belongs_to :item :belongs_to :buyer .... Then we can use: Item.first.buyers , Buyers.first.items and Buyer

Django dumpdata and loaddata not working for many-to-many intermediary model

吃可爱长大的小学妹 提交于 2019-12-12 06:09:06
问题 I am using dumpdata with Django 1.2.3 on the following model: class Bar(models.Model): ... class Foo(models.Model): bars = models.ManyToManyField(Bar, through="Foo_bar", blank=True, null=True) ... class Foo_bar(models.Model): foo = models.ForeignKey(Foo) bar = models.ForeignKey(Bar) status = models.IntegerField() ... The json fixture serializes the bars associated with Foos in the Foo objects, resulting in an AttributeError when I try to run loaddata with the fixture: AttributeError: Cannot

Django ManyToMany CreateView Fields In Both Tables

梦想与她 提交于 2019-12-12 05:48:26
问题 I have two models, there are Book and Author and I add ManyToMany field inside Book model class Author(models.Model): name = models.CharField(verbose_name='name', max_length=50) created_at = models.DateTimeField(auto_now_add=True) def __unicode__(self): return unicode(self.name) class Book(models.Model): title = models.CharField(verbose_name='title', max_length=50) authors = models.ManyToManyField(Author) # Many to many created_at = models.DateTimeField(auto_now_add=True) def __unicode__(self