Sequelize upsert()永远不会更新,只会插入

时间:2016-09-08 19:43:14

标签: javascript sql postgresql sequelize.js upsert

所以我尝试使用model.upsert()的{​​{1}},我收到的只是插入内容,无论我在查询中发生什么变化。

我有一个包含一些字段的Transaction模型,默认生成id。

阅读sequelize的sequelize文档,我注意到了这一点:

  

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

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

id

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

任何解释和解决方案都将受到高度赞赏,提前感谢!

编辑:

这是upsert代码:

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

因为这是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 }) } } Mutation的一部分。

可能值得注意的是,此前GraphQLaddTransaction而我所有更改的内容均来自db.models.transaction.upsert()的{​​{1}}

1 个答案:

答案 0 :(得分:3)

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