loopback-next
loopback-next copied to clipboard
Setting a foreign key as a nullable key in model file is not working in loopback 4
Describe the bug
I want to set up a field that is defined as a foreign key, but also supports the ability to be null. In the model file, I have this already
@belongsTo(() => Bill, { keyTo: 'BillID', name: 'Bill' })
BillID: Number;
But what I want to do is something like this
@property({
type: Number,
required: false,
jsonSchema: { nullable: true },
mssql: { "columnName": "BillID", "dataType": "int", "dataPrecision": null, "dataScale": null, "nullable": "YES" },
})
@belongsTo(() => Bill, { keyTo: 'BillID', name: 'Bill' })
BillID: Number;
but that does not work. How can I set this up?
Logs
No response
Additional information
No response
Reproduction
not needed as problem is obvious
Hi, I've been using LoopBack for just one week.
I noticed that the "nullable" option should be placed outside of the "mysql" configuration here. I'm not sure if this is a version issue or something else.
Also, I tried your code and found that I shouldn't use @property together with @belongsTo.