While attempting to utilize model.upsert()
within sequelize
, I am consistently experiencing insertions, regardless of any modifications made to the query.
I have a Transaction model that comprises various fields, including the default generated id.
Upon reviewing the documentation for upsert
in sequelize, it became apparent that:
An update will occur if a row matching the supplied values on either the primary key or a unique key is discovered. It's important to define the unique index in your sequelize model rather than solely in the table.
My assumption was that defining the id
of the Transaction in the model definition would be necessary, so I proceeded accordingly without success as it continues to create new entries exclusively...
TransactionModel = {
id: {
type: Sequelize.INTEGER,
allowNull: false,
primaryKey: true,
autoIncrement: true
},
{.......}
}
Where could I be going wrong? What might I have overlooked?
Any insights and resolutions would be greatly appreciated. Thank you in advance!
EDIT:
The upsert code utilized is:
createOrUpdateTransaction: {
type: Transaction,
args: {
payerAccountNumber: {type: new GraphQLNonNull(GraphQLInt)},
recipientAccountNumber: {type: new GraphQLNonNull(GraphQLInt)},
amount: {type: new GraphQLNonNull(GraphQLFloat)},
currency: {type: new GraphQLNonNull(GraphQLString)},
paymentMethod: {type: new GraphQLNonNull(GraphQLString)},
cardNumber: {type: GraphQLFloat},
cardName: {type: GraphQLString},
cardNetwork: {type: GraphQLString},
cashMachineId: {type: GraphQLFloat},
receiptNumber: {type: new GraphQLNonNull(GraphQLFloat)},
invoiceNumber: {type: new GraphQLNonNull(GraphQLFloat)},
receiptCopy: {type: new GraphQLNonNull(GraphQLString)},
description: {type: GraphQLString},
bankDescription: {type: GraphQLString},
bankReference: {type: new GraphQLNonNull(GraphQLString)},
bankSubCurrencyAccount: {type: new GraphQLNonNull(GraphQLString)},
tags: {type: new GraphQLList(GraphQLString)},
notes: {type: GraphQLString}
},
resolve: (root, args) => {
return db.models.transaction.upsert({
time: new Date().toString(),
payerAccountNumber: args.payerAccountNumber,
recipientAccountNumber: args.recipientAccountNumber,
amount: args.amount,
currency: args.currency,
paymentMethod: args.paymentMethod,
cardNumber: args.cardNumber,
cardName: args.cardName,
cardNetwork: args.cardNetwork,
cashMachineId: args.cashMachineId,
receiptNumber: args.receiptNumber,
invoiceNumber: args.invoiceNumber,
receiptCopy: args.receiptCopy,
description: args.description,
bankDescription: args.bankDescription,
bankReference: args.bankReference,
bankSubCurrencyAccount: args.bankSubCurrencyAccount,
tags: args.tags,
notes: args.notes,
bankAccountAccountNumber: args.payerAccountNumber
})
}
}
This is part of a Mutation
within GraphQL
.
It should be noted that this previously functioned as addTransaction
, and the only alteration made was switching from db.models.transaction.create()
to db.models.transaction.upsert()
.