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.

Create foreign key constraint without relation

See original GitHub issue

Issue type:

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

Database system/driver:

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

TypeORM version:

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

I’m evaluating TypeORM and I’m trying to figure out if there is a way to define a foreign key constraint without having to define the relationship. All I care about is that the database enforces the constraint - I don’t want or need the persistence, loading of relations, or the superfluous properties on my classes. For example:

// A user can have multiple phone numbers
@Entity()
class PhoneNumber {
    @PrimaryGeneratedColumn(); 
    id: number; 
    
    @Column()
    @ForeignKey((u: User) => u.id)
    userId: string;

    @Column(); 
    phoneNumber: string; 
}

This would create a foreign key constraint, and nothing else. Is this possible somehow? I found a couple other posts requesting the same thing, but I’m not sure if it’s possible. https://github.com/typeorm/typeorm/issues/188 https://stackoverflow.com/questions/55170906/typeorm-how-to-set-foreignkey-explicitly-without-having-property-for-loading-re

Thanks in advance.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:38
  • Comments:17

github_iconTop GitHub Comments

15reactions
jb4ecommented, Feb 25, 2020

Thanks, @diegoazh, but I don’t think that addresses what I’m looking for. I’d like to be able to specify only profileId on the model, and not have a profile property. Something like this:

    @Column({ nullable: true })
    @ForeignKey(() => Profile)
    profileId: number;
4reactions
DaichiShirakawacommented, Apr 23, 2021

Sharing my temporary solution for this issue:


@Entity("parents")
export class Parent {
  @PrimaryGeneratedColumn("uuid")
  id: string;

  // ⭐No relationship descriptions for Parent
}
@Entity("children")
export class Child extends {
  @PrimaryGeneratedColumn("uuid")
  id: string;
  @Column()
  parentId: string;

  // ⭐Only relation definitions in Child. It works!
  @ManyToOne(() => Parent)
  @JoinColumn({ name: "parentId" })
  _tenantRelation: Tenant; // ⭐Using underscored property, because I don't need it.
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

sqlalchemy: create relations but without foreign key constraint ...
1 Answer 1 · Note: the key is, no claim of foreign keys on a_id of class C. · Yes, as relationship is...
Read more >
Create Foreign Key Relationships - SQL Server | Microsoft Learn
This article describes how to create foreign key relationships in SQL Server by using SQL Server Management Studio or Transact-SQL.
Read more >
Operating without foreign key constraints - PlanetScale
A FOREIGN KEY constraint is a database construct, an implementation that forces the foreign key relationship's integrity (referential ...
Read more >
Foreign Key Constraint | CockroachDB Docs
The `FOREIGN KEY` constraint specifies a column can contain only values exactly matching existing values from the column it references.
Read more >
SQLite Foreign Key: Enforce Relationships Between Tables
If the SQLite library is compiled with foreign key constraint support, the application can use the PRAGMA foreign_keys command to enable or disable...
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