loopback-next icon indicating copy to clipboard operation
loopback-next copied to clipboard

Setting a foreign key as a nullable key in model file is not working in loopback 4

Open Employee87 opened this issue 3 years ago • 1 comments

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

Employee87 avatar Sep 28 '22 18:09 Employee87

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.

ocup0311 avatar Nov 09 '24 20:11 ocup0311