一尘不染

续集upsert()从不更新,仅插入

sql

因此,无论我在查询中进行了什么更改,我都尝试使用model.upsert()of,sequelize并且我收到的所有内容都是inserts。

我有一个包含某些字段的事务模型,具有默认的生成ID。

在阅读续集的upsert文档时,我注意到了这一点:

如果找到与主键或唯一键上提供的值匹配的行,则将执行更新。请注意,唯一索引必须在sequelize模型中定义,而不仅仅是在表中定义。

所以我猜想我必须id在模型定义中定义Transaction的名称,所以我没有幸免,因为它仍然只能创建新条目。

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

我做错了什么,我想念什么?

任何解释和解决方案将不胜感激,在此先感谢您!

编辑:

这是upsert代码:

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

由于这是Mutationin中的一部分GraphQL

可能值得注意的是,这是addTransaction以前的事,而我更改的只是db.models.transaction.upsert()db.models.transaction.create()


阅读 268

收藏
2021-05-23

共1个答案

一尘不染

在您的upsert()示例中,您没有提供upsert方法中条目的 ID 。这意味着sequelize无法将 id 与行匹配(因为 id
是未定义的),因此它会插入新行。

即使您使用其他主键,它也必须始终是要匹配的属性,因为sequelize使用主键搜索现有行。

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 ...
        })
    }
}
2021-05-23