簡體   English   中英

Sequelize upsert()永遠不會更新,只會插入

[英]Sequelize upsert() never updates and only inserts

所以我想用model.upsert()sequelize和所有我收到的插入,不管我在查詢更改。

我有一個Transaction模型,它有一些字段,默認生成id。

閱讀sequelize的upsert文檔,我注意到了這一點:

如果找到與主鍵或唯一鍵上提供的值匹配的行,則將執行更新。 請注意,必須在續集模型中定義唯一索引,而不是僅在表中定義。

所以我猜我必須在模型定義中定義Transaction的id ,所以我沒有運氣,因為它仍然只創建新的條目..

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

因為這是GraphQL MutationGraphQL

值得注意的是,這之前是addTransaction ,我改變的是db.models.transaction.upsert()db.models.transaction.create()

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM