Sequelize upsert() 从不更新,只插入
Sequelize upsert() never updates and only inserts
所以我正在尝试使用 sequelize
的 model.upsert()
,无论我在查询中更改什么,我收到的只是插入。
我有一个事务模型,它有一些字段,带有默认生成的 ID。
阅读续集的 upsert
文档我注意到了这一点:
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.
所以我猜我必须在模型定义中定义交易的 id
,所以我没有运气,因为它仍然只创建新条目..
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
})
}
}
因为这是 GraphQL
中 Mutation
的一部分。
可能值得注意的是,这是之前的 addTransaction
,我所做的只是从 db.models.transaction.create()
更改为 db.models.transaction.upsert()
在您的 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 ...
})
}
}
所以我正在尝试使用 sequelize
的 model.upsert()
,无论我在查询中更改什么,我收到的只是插入。
我有一个事务模型,它有一些字段,带有默认生成的 ID。
阅读续集的 upsert
文档我注意到了这一点:
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.
所以我猜我必须在模型定义中定义交易的 id
,所以我没有运气,因为它仍然只创建新条目..
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
})
}
}
因为这是 GraphQL
中 Mutation
的一部分。
可能值得注意的是,这是之前的 addTransaction
,我所做的只是从 db.models.transaction.create()
db.models.transaction.upsert()
在您的 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 ...
})
}
}