Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
134
rated 0 times [  139] [ 5]  / answers: 1 / hits: 17508  / 8 Years ago, thu, september 8, 2016, 12:00:00

So I'm trying to use the model.upsert() of sequelize and all i receive is inserts , no matter what i change in the query.



I have a Transaction model that has some fields, with the default generated id.



reading the sequelize's upsert documentation i noticed this:




An update will be executed if a row which matches the supplied values on either the primary key or a unique key is found. Note that the unique index must be defined in your sequelize model and not just in the table.




So i was guessing i have to define the id of the Transaction in the model definition, and so i did with no luck as it still only creates new entries..



TransactionModel = {
id: {
type: Sequelize.INTEGER,
allowNull: false,
primaryKey: true,
autoIncrement: true
},
{.......}
}


What am i doing wrong, what did i miss?



Any explanation and solution will be highly appreciated, thanks in advance!



EDIT:



This is the upsert code:



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
})
}
}


As this is part of a Mutation in GraphQL.



It might be worth noting that this was addTransaction before and all i changed was to db.models.transaction.upsert() from db.models.transaction.create()


More From » sql

 Answers
46

In your upsert() example you aren't providing the id of the entry into the upsert method. This means sequelize can't match the id to a row (because the id is undefined) and therefore it inserts a new row.



Even if you use a different primary key it must always be a property for it to match since sequelize uses the primary key to search for an existing row.



createOrUpdateTransaction: {
type: Transaction,
args: {
// Omitted code...
},
resolve: (root, args) => {
return db.models.transaction.upsert({
// The id property must be defined in the args object for
// it to match to an existing row. If args.id is undefined
// it will insert a new row.
id: args.id,
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,
// Omitted fields ...
})
}
}

[#60767] Tuesday, September 6, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
larrycodys

Total Points: 394
Total Questions: 93
Total Answers: 78

Location: Romania
Member since Mon, Jun 6, 2022
2 Years ago
;