Sequelize upsert() never updates and only inserts

故事扮演 提交于 2020-07-09 03:04:54

问题


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


回答1:


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


来源:https://stackoverflow.com/questions/39399018/sequelize-upsert-never-updates-and-only-inserts

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!