Sequelize upsert() 从不更新,只插入

Kes*_*vid 6 javascript sql postgresql upsert sequelize.js

所以我正在尝试使用model.upsert()ofsequelize并且我收到的所有内容都是 inserts ,无论我在查询中更改了什么。

我有一个事务模型,它有一些字段,带有默认生成的 id。

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

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

所以我猜我必须id在模型定义中定义事务的,所以我没有运气,因为它仍然只创建新条目..

TransactionModel = {
    id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        primaryKey: true,
        autoIncrement: true
    },
    {.......}
}
Run Code Online (Sandbox Code Playgroud)

我做错了什么,我错过了什么?

任何解释和解决方案将不胜感激,提前致谢!

编辑:

这是更新插入代码:

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
                })
            }
        }
Run Code Online (Sandbox Code Playgroud)

由于这是 a Mutationin 的一部分GraphQL

可能值得注意的是,这是addTransaction之前,我改变的是db.models.transaction.upsert()db.models.transaction.create()

gri*_*urd 6

在您的 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 ...
        })
    }
}
Run Code Online (Sandbox Code Playgroud)