如何解决Knex/PostgreSQL报错update is not a function in an upsert method

问题描述 投票:0回答:1

我正在尝试在 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',
    });
  }
postgresql knex.js
1个回答
0
投票

看起来您想在冲突后使用

.merge()
执行更新而不是
.update()
https://knexjs.org/guide/query-builder.html#merge

© www.soinside.com 2019 - 2024. All rights reserved.