question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Primary key as join column in 1:1 relation

See original GitHub issue

Issue type:

[x] question [?] bug report [ ] feature request [ ] documentation issue

Database system/driver:

[ ] cordova [ ] mongodb [ ] mssql [ ] mysql / mariadb [ ] oracle [x] postgres [ ] cockroachdb [ ] sqlite [ ] sqljs [ ] react-native [ ] expo

TypeORM version:

[ ] latest [ ] @next [x] 0.2.12

Steps to reproduce or a small repository showing the problem:

Let’s say I have the following two entities (simplified for demonstration purposes):

@Entity()
export class A {
  @PrimaryGeneratedColumn('uuid')
  readonly id: string;

  @OneToOne(() => B, b => b.a, { nullable: true })
  readonly b: B | null;
}

@Entity()
export class B {
  @PrimaryColumn()
  readonly id: string;

  @OneToOne(() => A, a => a.b)
  @JoinColumn({ name: 'id' })
  readonly a: A;
}

Running schema:log or migration:generate on a blank database shows that TypeORM generates both a primary key as well as a unique key for B.id. It’s clear why TypeORM would want to add a unique key constraint on a column which represents a 1:1 relation in general, but in this case the column already has a primary key and therefore I’d think the unique key is redundant. If my logic is sound then this is a bug 😃 it also happens if entity B already existed previously and now I’m adding entity A and changing B.id from a generated UUID primary column to a join column for the new relation - the migration that is generated drops the default value of B.id, creates the appropriate table for A and the foreign key B("id") references A("id"), but also creates the unique key while leaving the existing primary key in place. (One other thing it does is DROP SEQUENCE "b_id_seq", which never existed as B.id is a UUID column and not a serial, but that’s a separate issue.)

The question is therefore: is this the expected behaviour (as in “expected by design”)? If so, why is that? Why wouldn’t the primary constraint do the job? And if not, well, then like I said, I’d like to report a beetle sighting 😃

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:31
  • Comments:13 (1 by maintainers)

github_iconTop GitHub Comments

5reactions
bmoe24xcommented, Jul 26, 2019

+1 we are having the same issue. Don’t want a redundant column if both entities are related based on their primary key.

2reactions
roylee0704commented, Dec 6, 2019

@jahudka try specify classB.id column as type ‘uuid’

@Entity()
export class A {
  @PrimaryGeneratedColumn('uuid')
  readonly id: string;

  @OneToOne(() => B, b => b.a, { nullable: true })
  readonly b: B | null;
}

@Entity()
export class B {

  @PrimaryColumn('uuid')
  readonly id: string;

  @OneToOne(() => A, a => a.b)
  @JoinColumn({ name: 'id' })
  readonly a: A;
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

JPA: difference between @JoinColumn and ... - Stack Overflow
The PrimaryKeyJoinColumn annotation specifies a primary key column that is used as a foreign key to join to another table.
Read more >
SQL join two tables related by a single column primary key or ...
In this page, we are going to discuss the usage of two or more tables in a joining with single column PRIMARY KEY...
Read more >
How to change the @OneToOne shared primary key column ...
In this article, we are going to see how you can change the @OneToOne shared primary key column name when using JPA and...
Read more >
Chapter 2. Mapping Entities - Red Hat on GitHub
The foreign key name(s) referencing the owner table is the concatenation of the owner table, _, and the owner primary key column(s) name....
Read more >
Hibernate Many-to-Many Association with Extra Columns in ...
many to many relationship updated 2. We recommend this solution because it is simple and using the separate primary key in the join...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found