Questions tagged [one-to-one]

Refers to the quantity of an entity as a relationship to another entity. Meaning that for every instance of an entity, there is a single related instance of another entity.

Refers to the quantity of an entity as a relationship to another entity.

Meaning that for every instance of an entity, there is a single related instance of another entity.

1496 questions
252
votes
12 answers

How can I make a JPA OneToOne relation lazy

In this application we are developing, we noticed that a view was particularly slow. I profiled the view and noticed that there was one query executed by hibernate which took 10 seconds even if there only were two object in the database to fetch.…
KCL
  • 6,733
  • 10
  • 37
  • 43
184
votes
26 answers

Is there ever a time where using a database 1:1 relationship makes sense?

I was thinking the other day on normalization, and it occurred to me, I cannot think of a time where there should be a 1:1 relationship in a database. Name:SSN? I'd have them in the same table. PersonID:AddressID? Again, same table. I can come…
Pulsehead
  • 5,050
  • 9
  • 33
  • 37
103
votes
2 answers

Whats the difference between a OneToOne, ManyToMany, and a ForeignKey Field in Django?

I'm having a little difficulty getting my head around relationships in Django models. Could someone explain what the difference is between a OneToOne, ManyToMany and ForeignKey?
99
votes
8 answers

Check if OneToOneField is None in Django

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…
John Bright
  • 991
  • 1
  • 6
  • 5
81
votes
7 answers

One to one optional relationship using Entity Framework Fluent API

We want to use one to one optional relationship using Entity Framework Code First. We have two entities. public class PIIUser { public int Id { get; set; } public int? LoyaltyUserDetailId { get; set; } public LoyaltyUserDetail…
81
votes
2 answers

Hibernate: one-to-one lazy loading, optional = false

I faced the problem that one-to-one lazy loading doesn't work in hibernate. I've already solved it, but still don't properly understand what happens. My code (lazy loading doesn't work here, when I pull Person - Address is also…
VB_
  • 45,112
  • 42
  • 145
  • 293
80
votes
5 answers

Django Admin: OneToOne Relation as an Inline?

I'm putting together the admin for a satchmo application. Satchmo uses OneToOne relations to extend the base Product model, and I'd like to edit it all on one page. Is it possible to have a OneToOne relation as an Inline? If not, what is the best…
Jiaaro
  • 74,485
  • 42
  • 169
  • 190
78
votes
2 answers

JPA @OneToOne with Shared ID -- Can I do this Better?

I’m working with an existing schema that I’d rather not change. The schema has a one-to-one relationship between tables Person and VitalStats, where Person has a primary key and VitalStats uses the same field as both its primary key and its foreign…
Michael
  • 1,351
  • 1
  • 11
  • 25
78
votes
8 answers

How to Create a real one-to-one relationship in SQL Server

I have two tables Country and Capital, I set Capital's primary key as foreign key which references Country's primary. But when I use Entity Framework database-first, the model is 1 to 0..1. How does one create a one-to-one relationship in SQL…
James
  • 2,570
  • 7
  • 34
  • 57
69
votes
3 answers

MySQL - One To One Relationship?

I'm trying to achieve a "One to one" relationship in a MySQL database. For example, let's say I have a Users table and an Accounts table. I want to be sure that a User can have only one Account. And that there can be only one Account per User. I…
Limeni
  • 4,954
  • 8
  • 31
  • 33
55
votes
4 answers

Hibernate ManyToOne vs OneToOne

I can't see any difference in the schema of a Many-To-One relationship vs a OneToOne relationship: @Entity public class Order { @ManyToOne @JoinColumn(nullable = false) private Address address; vs @Entity public class Order { …
DD.
  • 21,498
  • 52
  • 157
  • 246
54
votes
1 answer

django OneToOne reverse access

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 =…
WindowsMaker
  • 3,132
  • 7
  • 29
  • 46
52
votes
2 answers

Hibernate : attempted to assign id from null one-to-one property: employee

This is my Database structure, One-to-One mapping in MySQL: This is my java file: public class Employee { private EmployeeDetail empdetail; private String firstname; private String lastname; // getters and setters } public class…
Pawan
  • 31,545
  • 102
  • 256
  • 434
38
votes
5 answers

Check if a OneToOne relation exists in Django

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 =…
Alfred Huang
  • 17,654
  • 32
  • 118
  • 189
36
votes
6 answers

Why use a 1-to-1 relationship in database design?

I am having a hard time trying to figure out when to use a 1-to-1 relationship in db design or if it is ever necessary. If you can select only the columns you need in a query is there ever a point to break up a table into 1-to-1 relationships. I…
chobo
  • 31,561
  • 38
  • 123
  • 191
1
2 3
99 100