loopbackio / loopbackio/loopback-next

[@loopback/sequelize] Duplicate column names in relation queries

Aperta
#9,617 2 commenti 1 reazione 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

bug sequelize
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:
![loopback_relation_data](https://github.com/loopbackio/loopback-next/assets/5942028/5f9e6897-65ec-419c-a73c-ba11fe84193c)
- This leads to undefined being passed as the foreign key to sequelize:
![undefined_foreign_key](https://github.com/loopbackio/loopback-next/assets/5942028/bc3cea83-b6fe-4664-b97e-d722a01d07cb)
- 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:
![duplicate_column_sqlite_error](https://github.com/loopbackio/loopback-next/assets/5942028/8cff7599-28ac-4368-93fe-5d8254ff1778)

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. 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

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.