我正在尝试在 KnexJS 中做一个带有 upsert 方法的种子,该方法应该插入新数据并在下次种子运行时更新相同的数据。
Kex版本
[email protected]
和使用PG
客户端[email protected]
我需要保持同一行为什么种子需要这样做
我做了什么
/**
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.seed = async function (knex) {
// Inserts a new questionnaire or updates an existing one
const [questionnaire] = await knex('questionnaires')
.insert({
title: 'User Consent Questionnaire',
description:
'This questionnaire is used to obtain user consent to the privacy',
eligibility_criteria: '[{"privacy_policy":"yes"}]',
created_by: '0',
type: 'USER_CONSENT',
// locale: 'en_US', by default is already as en_US
status: 'ACTIVE',
// study_id: '', by default is NULL
})
.onConflict(['type'])
// Running the seed again the same row will be updated but with new values
// The values can be edited right now are used as a starting point
// to ensure the correct functionality of the seed
.update({
title: 'Privacy Consent',
description: 'The user consenting to the privacy agreement',
// eligibility_criteria: '[{}]'
// created_by: '0',
// locale: 'en_US',
// study_id: '', by default is NULL
})
.returning('id');
const questionnaire_id = questionnaire.id;
// Inserts a new question or updates an existing one
await knex('questions')
.insert({
questionnaire_id,
question: 'Do you consent to the privacy requirements?',
description: 'privacy consent',
type: 'yesno',
key: 'privacy_policy',
sort_order: 0,
})
.onConflict(['questionnaire_id', 'key'])
.update({
question: 'Do you agree to the privacy policy?',
description: 'Privacy policy agreement',
});
return await knex('assigned_questionnaires').insert({
questionnaire_id,
eligible: 'NEVER_CHECKED',
});
};
这是我面临的错误
Error while executing "/test/seeds/adding_default_user_consent_questionnaire.js"
seed: knex(...).insert(...).onConflict(...).update is not a function
我还分享了在不使用
onConflict
的情况下与我一起工作的解决方案,但我想了解上述解决方案有什么问题
/**
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.seed = async function (knex) {
let questionnaire_id;
// Check if a USER_CONSENT questionnaire already exists
let [questionnaire] = await knex('questionnaires')
.where({ type: 'USER_CONSENT' })
.select('id');
// If the questionnaire exists, update it; otherwise, insert a new one
if (questionnaire) {
questionnaire_id = questionnaire.id;
await knex('questionnaires')
.where({ id: questionnaire_id })
.update({
title: 'User Consent Questionnaire',
description: 'This questionnaire is used to obtain user consent to the privacy',
eligibility_criteria: '[{"agree_privacy":"yes"}]',
type: 'USER_CONSENT',
status: 'ACTIVE',
});
} else {
[{ id: questionnaire_id }] = await knex('questionnaires')
.insert({
title: 'User Consent Questionnaire',
description: 'This questionnaire is used to obtain user consent to the privacy',
eligibility_criteria: '[{"agree_privacy":"yes"}]',
type: 'USER_CONSENT',
status: 'ACTIVE',
})
.returning('id');
}
// Check if the consent question already exists
let [question] = await knex('questions')
.where({
questionnaire_id,
key: 'agree_privacy'
})
.select('id');
// If the question exists, update it; otherwise, insert a new one
if (question) {
await knex('questions')
.where({ id: question.id })
.update({
questionnaire_id,
question: 'Do you consent to the privacy requirements?',
description: 'privacy consent',
type: 'yesno',
key: 'agree_privacy',
sort_order: 1,
});
} else {
await knex('questions')
.insert({
questionnaire_id,
question: 'Do you consent to the privacy requirements?',
description: 'privacy consent',
type: 'yesno',
key: 'agree_privacy',
sort_order: 1,
});
}
// Check if the assigned questionnaire already exists
let [assignedQuestionnaire] = await knex('assigned_questionnaires')
.where({ questionnaire_id })
.select('id');
// If the assigned questionnaire exists, update it; otherwise, insert a new one
if (assignedQuestionnaire) {
return await knex('assigned_questionnaires')
.where({ id: assignedQuestionnaire.id })
.update({
questionnaire_id,
eligible: 'NEVER_CHECKED',
});
} else {
return await knex('assigned_questionnaires').insert({
questionnaire_id,
eligible: 'NEVER_CHECKED',
});
}
• 'TypeError: is not a function' instance method in Node.js
• using ajax 此路由不支持 POST 方法。支持的方法:GET、HEAD
• C# MongoDB Upsert object inside array
• postgres 中的 PARTITION BY HASH 分区提醒是如何计算的
• 在 method=request.getParameter("value") 上得到空值;
• 如何通过 ASP.NET Core 中的链接进行 POST
• 我试图在我的视图上显示从数据库中获取的数据,但我无法显示它。 Laravel 10.x /
• Firebase 错误:照片 URL 太长。 (React.js, React-Router, React-Router Action)
• Error: undefined method `[]' for nil:NilClass (NoMethodError)
• 在 React js 中集成网站上的 Gpay 意图流时,Gpay 应用程序未打开
• Trait 方法 App\Models\Traits\MediaTrait::registerMediaConversions 尚未应用