Create foreign key constraint without relation
See original GitHub issueIssue 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:
- Created 4 years ago
- Reactions:38
- Comments:17
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:
Sharing my temporary solution for this issue: