one-to-one

Check if a OneToOne relation exists in Django

心已入冬 提交于 2019-12-03 10:39:51
Now I'm using django 1.6 I have two models relates with a OneToOneField . class A(models.Model): pass class B(models.Model): ref_a = models.OneToOneField(related_name='ref_b', null=True) First see my code that points out the problem: a1 = A.objects.create() a2 = A.objects.create() b1 = B.objects.create() b2 = B.objects.create(ref_a=a2) # then I call: print(a1.ref_b) # DoesNotExist Exception raised print(a2.ref_b) # returns b2 print(b1.ref_a) # returns None print(b2.ref_a) # returns a2 Now the problem is, if I want to check a A object, to judge whether it exists a B objects referencing it. How

Hibernate JPA one-to-one saving child class entity

為{幸葍}努か 提交于 2019-12-03 09:31:56
I have a one-to-one relationship using PrimaryKeyJoinColumn annotated on the parent side. And now I want to save the child entity by itself. For example, I have Employee and EmpInfo as the child entity, I need to save EmpInfo (of course after setting the id property of the parent to it). However, when such an arrangement is used, I get an exception listed below... org.springframework.dao.InvalidDataAccessApiUsageException: detached entity passed to persist Any ideas why hibernate does not allow this? To be more clear, the code I have is below... ParentEntity: public class Employee { private

Rails one-to-one relationship

亡梦爱人 提交于 2019-12-03 06:14:00
问题 I have the following: class User < ActiveRecord::Base has_one :car, :class_name => 'Car', :foreign_key => 'user_id' class Car < ActiveRecord::Base belongs_to :worker, :class_name => 'User', :foreign_key => 'user_id' It is basically a one-to-one relationship between a user and a car. What I want is for the User to be able to have one and only one car. That implies the fact that if he creates a car assigned to him, he won't be able to create the second. How could this be done? 回答1: why don't

store many of relation 1:1 between various type of objects : decoupling & high performance

混江龙づ霸主 提交于 2019-12-03 05:11:28
问题 I have 300+ classes. They are related in some ways. For simplicity, all relation are 1:1. Here is a sample diagram. (In real case, there are around 50 relation-pairs.) Note: For some instances, some relation may not exist. For example, some hen s don't relate to any food . Note2: No link = never, e.g. every egg doesn't relate to any cage . Such relation will never be added/removed/queried. Question: How to store relation between them elegantly? All 4 of my ideas (below) seem to have

Django Admin: How to display value of fields with list_display from two models which are in oneToOne relation?

余生颓废 提交于 2019-12-03 03:04:32
I belive that the answer to my problem is simple, but I can't find it anywhere. Here is my predicament. I have two models: Member and MemberDetail, which are in oneToOne relation like this: class Member(models.Model): ID = models.AutoField(primary_key=True) FIRST_NAME = models.CharField('First name', max_length=50) LAST_NAME = models.CharField('Last name', max_length=50) def __unicode__(self): return u'%s %s' % (self.FIRST_NAME, self.LAST_NAME) class MemberDetail(models.Model): member = models.OneToOneField(Member, primary_key=True) DATE_OF_BIRTH = models.DateField('Date of birth') EMAIL =

Check if OneToOneField is None in Django

≡放荡痞女 提交于 2019-12-03 03:01:15
问题 I have two models like this: class Type1Profile(models.Model): user = models.OneToOneField(User, unique=True) ... class Type2Profile(models.Model): user = models.OneToOneField(User, unique=True) ... I need to do something if the user has Type1 or Type2 profile: if request.user.type1profile != None: # do something elif request.user.type2profile != None: # do something else else: # do something else But, for users that don't have either type1 or type2 profiles, executing code like that produces

Creating PostgreSQL tables + relationships - PROBLEMS with relationships - ONE TO ONE

痴心易碎 提交于 2019-12-03 01:24:50
So I am supposed to create this schema + relationships exactly the way this ERD depicts it. Here I only show the tables that I am having problems with: So I am trying to make it one to one but for some reason, no matter what I change, I get one to many on whatever table has the foreign key. This is my sql for these two tables. CREATE TABLE lab4.factory( factory_id INTEGER UNIQUE, address VARCHAR(100) NOT NULL, PRIMARY KEY ( factory_id ) ); CREATE TABLE lab4.employee( employee_id INTEGER UNIQUE, employee_name VARCHAR(100) NOT NULL, factory_id INTEGER REFERENCES lab4.factory(factory_id), PRIMARY

django OneToOne reverse access

老子叫甜甜 提交于 2019-12-02 23:22:59
I have these simple classes Class A(models.Model): ... Class Meta(models.Model): a = models.OnetoOneField(A, primary_key=True) width = models.IntegerField(default=100) but when I do a = A() meta = Meta() a.save() meta.a = a meta.save() print a.meta.width i get 'A' object has no attribute 'meta' Why is this? Am I using OneToOne wrong? if so how can i get the correct print statement? Thanks Define a related_name to call the reverse accessor. a = models.OneToOneField(A, related_name='foobar') # ... a.foobar 来源: https://stackoverflow.com/questions/11088901/django-onetoone-reverse-access

Rails one-to-one relationship

爷,独闯天下 提交于 2019-12-02 20:52:02
I have the following: class User < ActiveRecord::Base has_one :car, :class_name => 'Car', :foreign_key => 'user_id' class Car < ActiveRecord::Base belongs_to :worker, :class_name => 'User', :foreign_key => 'user_id' It is basically a one-to-one relationship between a user and a car. What I want is for the User to be able to have one and only one car. That implies the fact that if he creates a car assigned to him, he won't be able to create the second. How could this be done? why don't you just test before the user tries to add a car? if worker.car raise "sorry buddy, no car for you" else car =

store many of relation 1:1 between various type of objects : decoupling & high performance

孤街醉人 提交于 2019-12-02 19:33:24
I have 300+ classes. They are related in some ways. For simplicity, all relation are 1:1. Here is a sample diagram. (In real case, there are around 50 relation-pairs.) Note: For some instances, some relation may not exist. For example, some hen s don't relate to any food . Note2: No link = never, e.g. every egg doesn't relate to any cage . Such relation will never be added/removed/queried. Question: How to store relation between them elegantly? All 4 of my ideas (below) seem to have disadvantages. Here is a related question but with 1:N and only 1 relation. My poor solutions These are semi