簡體   English   中英

遷移失敗並出現錯誤:table.integer(...).references(...).on(...).OnDelete 不是 function Knex.js postgres

[英]migration failed with error: table.integer(…).references(…).on(…).OnDelete is not a function Knex.js postgres

我知道我有 postgres 工作,並且我在 knexfile.js 中有用戶名和密碼以及數據庫名稱。 psql 也在運行。 問題我在嘗試使用我的一張表運行此命令時遇到問題。 我嘗試了很多不同的步驟,喜歡添加 table 而不是 on (我讀到它是一個別名。我嘗試了 foreign() 而不是 integer() 沒有進展。請幫助或指出我知識中的一個缺陷。謝謝

knex migrate:latest

我收到此錯誤

 Using environment: development 

 migration failed with error: table.string(...).references(...).on(...).OnDelete is not a function

migration file "20200525002912_personal_todos.js" failed    

    TypeError: table.integer(...).references(...).on(...).OnDelete is not a function
        at TableBuilder._fn (/home/misterjoe/personal_projects/group-note-todo-api/db/migrations/20200525002912_personal_todos.js:5:59)

這是我的 personal_todos.js 文件遷移

exports.up = function(knex) {
  return knex.schema.createTable('personal_todos', (table)=>{
    table.increments('id')
    table.integer('user_id').references('id').on('users').OnDelete('CASCADE');
    table.boolean('active').notNullable();
    table.string('start_time')
    table.string('end_time')
    table.string('header')
    table.string('body')
    table.integer('container_index').notNullable()
    table.integer('container_item_index').notNullable()
    table.timestamps(true,true);
    table.boolean('private')
  })
};

exports.down = function(knex) {
  return knex.schema.dropTableIfExists('personal_todos')
};

這也是我的 users.js 表

exports.up = function(knex) {
  return knex.schema.createTable('users', (table)=>{
      table.increments('id');
      table.string('first-name');
      table.string('last-name');
      table.string('username');
      table.string('email');
      table.string('password');
      table.integer('age');
      table.string('bio');
      table.string('gender');
      table.string('personalsecret1');
      table.string('personalsecret2');
      table.string('personalsecret3');
      table.string('colorScheme');
      table.binary('img');
      table.timestamps(true,true);
      table.boolean('payed');
      table.boolean('active');
      table.boolean('friends_can_see_private');
  })
};

exports.down = function(knex) {
  return knex.schema.dropTableIfExists('users')
};

我最終通過刪除 onDelete() 並在運行 knex migrate:latest 后將其添加回來來修復它。 我添加了更多代碼,然后它就起作用了。

這是我希望它有幫助的代碼,這是步驟

  1. 刪除 onDelete() 運行遷移:最新
  2. 添加無符號()。 onDelete('CASCADE') 我也添加了 onUpdate('CASCADE')

然后運行 knex migrate:latest

暫無
暫無

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

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