loopbackio / loopbackio/loopback-next
[@loopback/sequelize] Duplicate column names in relation queries
Nessuno ha ancora preso questa issue.
- Lingua principale
- TypeScript
- Stelle
- 5.1k
- Fork
- 1.1k
- Merge medio
- 2g 21h
- PR unite (30g)
- 27
Descrizione
### Describe the bug
Repository queries with relations that do not explicitly set all the keyTo / keyFrom / through properties will lead to broken SQL queries that contain duplicated title case column names mixed in with the expected camelcase columns.
Example:
```sql
SELECT bookid as bookId, reader.id as readerId, ReaderId …
```
Relates to:
https://github.com/sequelize/sequelize/issues/9328
https://github.com/loopbackio/loopback-next/issues/9591
https://github.com/sourcefuse/loopback4-sequelize/issues/35
### Logs
_No response_
### Additional information
Workaround seems to be to go through all the Entity relations and explicitly set all the relation key names but that can be time-consuming and error-prone with a larger project. It seems like it might be possible to mitigate this at the loopback model to Sequelize relation layer.
### Reproduction
With these changes, running the tests for the Sequelize extension will replicate the error:
https://github.com/KalleV/loopback-next/commit/3c29d852b46f19f4805d556ae32aee49c79471f1
Turns out it's necessary to define additional "belongsTo" relations in other entities before this happens. The extra relation is added to the "Patient" entity in this case. With this set up, I am seeing the following happen:
- The default relation properties are set by Loopback (i.e. I can see the "keyFrom" is populated as todoListId) but the "keyTo" is undefined:

- This leads to undefined being passed as the foreign key to sequelize:

- And then that causes Sequelize to assign a Title Case property through it's own default relation column logic leading to a duplicate column name in the database query:

Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia con i test dell’estensione Sequelize e le modifiche per la riproduzione nel commit 3c29d852b46f19f4805d556ae32aee49c79471f1. Esamina la configurazione della relazione Patient e la gestione di keyFrom, keyTo e through prima che la query raggiunga Sequelize. Il lavoro è completato quando le relazioni senza tutte le proprietà impostate esplicitamente non producono più colonne duplicate, con una copertura di regressione nei test dell’estensione.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- node.js, typescript
- Ambito
- api, backend, database
- Tipo di issue
- Bug
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 35/100