loopbackio / loopbackio/loopback-next

Loopback Connector Disregards Property Name

Open
#1,837 9 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

developer-experience good first issue help wanted Juggler
Dominant language
TypeScript
Stars
5.1k
Forks
1.1k
Avg merge
2d 21h
Merged PRs (30d)
27

Description

## Description / Steps to reproduce / Feature proposal
The @ property decorator has a name property which should specify the column name in the database in case it does not match with the field name in your model. However, the code in loopback-connector/sql.js does not utilize this metada so you only get the field name as it's defined in your model. In my case, I am using DB2 and this causes issues because the generated SQL select statement uses double-quotes around each field signify to DB2 that the field name matches exactly with what is specified in the DB (i.e. case sensitive).

To Reproduce:
Create a DB2 database w

1. ith a table "DRIVER" that contains field "DRIVER_ID".
2. Setup a basic Loopback4 project like the Todo Tutorial (replacing instances of Todo with Driver) but use a DB2 based datasource.
3. Create the Model like the following:

```typescript
@model({
name: 'DRIVER', // This corrects the case-sensitive issue at the Table level.
}
})
export class Driver extends Entity {
@property({
name: 'DRIVER_ID', // This is ignored by field mapping functions.
type: 'number',
id: true,
})
fooId?: number;
```

4. Use VS Code to start the server in Debug and issue a GET against drivers
5. In the sql.js --> buildColumnNames note that the name is 'fooId' instead of 'DRIVER_ID'
6. Continue execution, you will receive an SQL0206N error from DB2: "fooId" is not valid in the context where it used.

## Current Behavior
sql.js simply uses the field names as the DB column names.

## Expected Behavior
sql.js should map to the @ property 'name' property to determine the correct column names.

## Connectors to fix

- [x] SQL connectors - done by strongloop/loopback-connector#142
- [ ] MongoDB https://github.com/strongloop/loopback-connector-mongodb
- [ ] CouchDB2 https://github.com/strongloop/loopback-connector-couchdb2
- [ ] Cloudant https://github.com/strongloop/loopback-connector-cloudant (may depend on couchdb2)
- [ ] Cassandra https://github.com/strongloop/loopback-connector-cassandra

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in loopback-connector/sql.js at buildColumnNames and compare its field-name handling with the property name metadata shown in the reproduction. Check the referenced SQL connector change before modifying this issue. Done means generated SELECT columns use DRIVER_ID for the fooId property and the DB2 reproduction no longer raises SQL0206N.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
database
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.