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.

Error on Referencing a relation column in Unique constraint.

See original GitHub issue

I’m trying to add a Unique constraint with 2 columns where one column is the id column of the same entity and second column is the id column of the @ManyToOne relation column.

This isn’t working and TypeORM throws error: Error: Index contains column that is missing in the entity: albumId

Here’s my class:

@Entity()
@Unique('test_index', ['id', 'albumId'])
export class Photo {

  @PrimaryGeneratedColumn()
  id: number;

  @ManyToOne(type => Album, album => album.photos)
  album: Album;

What I am trying to achieve is to create a Unique index on combination of 2 columns where 1 column (albumId) is supposedly the foreign key column generated by TypeORM.

What’s the correct way to reference the albumId column in Unique index?

I also tried @Unique('test_index', ['id', 'album.albumId']) but that didn’t work either.

Thanks!

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:

[x] latest [ ] @next [ ] 0.x.x (or put your version here)

Steps to reproduce or a small repository showing the problem:

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6

github_iconTop GitHub Comments

2reactions
dan-klassoncommented, Jul 23, 2020

Just exclude the Id part.

2reactions
hcesarcommented, Apr 8, 2019

You should declare your albumId column (it doesn’t interfere on the relation).

You can declare it as private if you are not going to use it:

@Column({ nullable: true })
private albumId: number;
Read more comments on GitHub >

github_iconTop Results From Across the Web

Self Referencing table : UNIQUE constraint failed (SQL)
It is all because in Relation table you have PRIMARY KEY(ID_SOURCE, ID_DESTINATION) so this both values have to create unique set.
Read more >
3 common foreign key mistakes (and how to avoid them)
1. Mismatched data types ... When you're using a foreign key to reference a column in another table, the datatypes of both tables...
Read more >
postgresql - Error: pq: 1 columns must reference exactly 1 ...
Error : pq: 1 columns must reference exactly 1 columns in referenced table (found 2) [closed] · Why is name part of the...
Read more >
Unique Constraints and Check Constraints - SQL Server
If a UNIQUE constraint is added to a column that has duplicated values, the Database Engine returns an error and does not add...
Read more >
5 Examples of a UNIQUE Constraint in a Relational Database
NOT NULL constraints are defined at the column level; they do not allow NULL values in the column when the row is inserted...
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