loopbackio / loopbackio/loopback-next

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

Open
#8,957 1 comment 0 reactions 0 assignees View on GitHub
bug
Dominant language
TypeScript
Stars
5.1k
Forks
1.1k
Avg merge
2d 21h
Merged PRs (30d)
27

Description

### 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

Contributor guide

Open the contributing guide

Research direction

The issue identifies a model file containing the @property and @belongsTo declarations for BillID; start by tracing how LoopBack 4 represents nullable foreign keys. Done would be a confirmed supported configuration or a clarified framework change, with nullable BillID behavior verified.

Written by the indexing model from the issue text.

Assessment

Tech stack
nodejs, typescript
Domain
api, backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.