-
Notifications
You must be signed in to change notification settings - Fork 286
Three foreign keys referencing same table #266
New issue
Have a question about this project? No Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “No Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? No Sign in to your account
Comments
Hey @devuxer, sry for the late response. You need to define which foreign key should be used for which relation. So the relations should look like: @BelongsTo(() => Nation, { onDelete: "no action", foreignKey: "originNationId"})
originNation: Nation;
@BelongsTo(() => Nation, { onDelete: "no action", foreignKey: "destinationNationId" })
destinationNation: Nation;
@BelongsTo(() => Nation, { onDelete: "no action", foreignKey: "vesselFlagNationId" })
vesselFlagNation: Nation; Probably an error message in case of multiple foreignKey that targets the same Model would be helpful 🤔 |
Hey @RobinBuschmann, Thank you for the answer and this very handy library 👍 An error message would definitely help. Perhaps adding this multiple-foreign-key use case to the documentation as well. One thing that confuses me is why I need |
This is because Ones might consider extending the functionalities of the |
Ahh OK, makes sense. So if I want the foreign key to be nullable, I would put |
This is because @foreignkey is more a helper for the @BelongsTo, @BelongsToMany and so on annotations then an independent decorator. In your case, since you need to define the foreign keys in the @BelongsTo annotation, you can omit the foreign key annotated properties completely. Ones might consider extending the functionalities of the @foreignkey decorator... |
@devuxer I've completely overseen your last question - sry for that. You need to set @BelongsTo(() => Nation, {
foreignKey: {
allowNull: true
}
}) |
Hello @RobinBuschmann Thank you a lot for answers about several FK referencing same table. But can you explain how to implement model association in this case? (getters and setters methods) @BelongsTo(() => User, { foreignKey: {
name: "createUserId",
allowNull: true
} })
createUser: User;
@BelongsTo(() => User, { foreignKey: {
name: "updateUserId",
allowNull: true
} })
updateUser: User;
public getCreateUser!: BelongsToGetAssociationMixin<User>;
public getUpdateUser!: BelongsToGetAssociationMixin<User>; //--What field will be used?-- |
how to defin hasmany at National table? |
My
Transit
model has three foreign keys referencing theNation
model.When I sync to a SQLite database, however, only the first foreign key gets created.
Am I doing something wrong?
Model
Expected Result (3 references to "Nations")
Actual Result (1 reference to "Nations")
The text was updated successfully, but these errors were encountered: