Skip to content

Commit bd668e8

Browse files
committed
fix: correct Lucid model mappings to match database schema
This fixes 'external_id', 'card_last_numbers', 'transaction_id', and 'product_id' mappings, and ensures many-to-many relationships use the correct pivot keys.
1 parent 996a6b2 commit bd668e8

3 files changed

Lines changed: 13 additions & 4 deletions

File tree

app/models/product.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,12 @@ export default class Product extends BaseModel {
1515

1616
@manyToMany(() => Transaction, {
1717
pivotTable: 'transaction_products',
18+
localKey: 'id',
19+
pivotForeignKey: 'product_id',
20+
relatedKey: 'id',
21+
pivotRelatedForeignKey: 'transaction_id',
1822
pivotColumns: ['quantity'],
23+
pivotTimestamps: true,
1924
})
2025
declare transactions: ManyToMany<typeof Transaction>
2126

app/models/transaction.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export default class Transaction extends BaseModel {
1515
@column({ columnName: 'gateway' })
1616
declare gatewayId: number | null
1717

18-
@column()
18+
@column({ columnName: 'external_id' })
1919
declare externalId: string | null
2020

2121
@column()
@@ -24,7 +24,7 @@ export default class Transaction extends BaseModel {
2424
@column()
2525
declare amount: number
2626

27-
@column()
27+
@column({ columnName: 'card_last_numbers' })
2828
declare cardLastNumbers: string | null
2929

3030
@belongsTo(() => Client)
@@ -35,6 +35,10 @@ export default class Transaction extends BaseModel {
3535

3636
@manyToMany(() => Product, {
3737
pivotTable: 'transaction_products',
38+
localKey: 'id',
39+
pivotForeignKey: 'transaction_id',
40+
relatedKey: 'id',
41+
pivotRelatedForeignKey: 'product_id',
3842
pivotColumns: ['quantity'],
3943
pivotTimestamps: true,
4044
})

app/models/transaction_product.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ export default class TransactionProduct extends BaseModel {
55
@column({ isPrimary: true })
66
declare id: number
77

8-
@column()
8+
@column({ columnName: 'transaction_id' })
99
declare transactionId: number
1010

11-
@column()
11+
@column({ columnName: 'product_id' })
1212
declare productId: number
1313

1414
@column()

0 commit comments

Comments
 (0)