Skip to content

Create foreign key constraint without relation #4569

@jb4e

Description

@jb4e

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.
#188
https://stackoverflow.com/questions/55170906/typeorm-how-to-set-foreignkey-explicitly-without-having-property-for-loading-re

Thanks in advance.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions