顺序迁移:关系< table>不存在 [英] Sequelize Migration: relation <table> does not exist

查看:119
本文介绍了顺序迁移:关系< table>不存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过Author hasmany Books示例工作,并尝试运行sequelize-cli迁移,但是在运行以下迁移时遇到以下问题:

I'm working through an Author hasMany Books example and am attempting to run a sequelize-cli migration, but am getting the following issue when I run the following migration:

ERROR: relation "authors" does not exist

此是创建作者的第一个迁移:

This is the first migration to create an author:

'use strict';
module.exports = {
  up: (queryInterface, Sequelize) => {
    return queryInterface.createTable('Authors', {
      id: {
        allowNull: false,
        autoIncrement: true,
        primaryKey: true,
        type: Sequelize.INTEGER
      },
      firstName: {
        type: Sequelize.STRING
      },
      lastName: {
        type: Sequelize.STRING
      },
      dateOfBirth: {
        type: Sequelize.DATEONLY
      },
      dateOfDeath: {
        type: Sequelize.DATEONLY
      },
      createdAt: {
        allowNull: false,
        type: Sequelize.DATE
      },
      updatedAt: {
        allowNull: false,
        type: Sequelize.DATE
      }
    });
  },
  down: (queryInterface, Sequelize) => {
    return queryInterface.dropTable('Authors');
  }
};

创建图书的第二次迁移:

The second migration to create a book:

'use strict';
module.exports = {
  up: (queryInterface, Sequelize) => {
    return queryInterface.createTable('Books', {
      id: {
        allowNull: false,
        autoIncrement: true,
        primaryKey: true,
        type: Sequelize.INTEGER
      },
      title: {
        type: Sequelize.STRING
      },
      summary: {
        type: Sequelize.STRING
      },
      isbn: {
        type: Sequelize.STRING
      },
      createdAt: {
        allowNull: false,
        type: Sequelize.DATE
      },
      updatedAt: {
        allowNull: false,
        type: Sequelize.DATE
      }
    });
  },
  down: (queryInterface, Sequelize) => {
    return queryInterface.dropTable('Books');
  }
};

迁移以创建Author和Book之间的关系:

The migration to create the relationship between Author and Book:

'use strict';

module.exports = {
  up: (queryInterface, Sequelize) => {
    return queryInterface.addColumn(
      'Books', // name of source model
      'AuthorId',
      {
        type: Sequelize.INTEGER,
        references: {
          model: 'authors',
          key: 'id'
        },
        onUpdate: 'CASCADE',
        onDelete: 'SET NULL'
      }
    )
  },

  down: (queryInterface, Sequelize) => {
    return queryInterface.removeColumn(
      'Books',
      'AuthorId'
    )
  }
};

这些是我的模型:

author.js:

author.js:

'use strict';
module.exports = (sequelize, DataTypes) => {
  var Author = sequelize.define('Author', {
    firstName: { type: DataTypes.STRING, allowNull: false, len: [2, 100] },
    lastName: { type: DataTypes.STRING, allowNull: false },
    dateOfBirth: { type: DataTypes.DATEONLY },
    dateOfDeath: { type: DataTypes.DATEONLY }
  }, {});
  Author.associate = function (models) {
    // associations can be defined here
    Author.hasMany(models.Book);
  };
  return Author;
};

book.js:

'use strict';
module.exports = (sequelize, DataTypes) => {
  var Book = sequelize.define('Book', {
    title: { type: DataTypes.STRING, allowNull: false, len: [2, 100], trim: true },
    summary: { type: DataTypes.STRING, allowNull: false },
    isbn: { type: DataTypes.STRING, allowNull: false }
  }, {});

  Book.associate = function (models) {
    // associations can be defined here
    Book.belongsTo(models.Author);
  };
  return Book;
};

我尝试了各种方法都无济于事。我的猜测是,它正在尝试以异步方式更改表,但先前的迁移已运行并完成:

I've tried all sorts of things to no avail. My guess would be that it is attempting to alter the table in an asynchronous manner, but the previous migrations ran and finished:

我正在使用以下内容:

"pg": "^7.4.3"
"sequelize": "^4.37.10"
"sequelize-cli": "^4.0.0"
 "express": "~4.16.0"

我是续作的新手,我们将不胜感激!

I'm very new to sequelize and any help would be appreciated!

推荐答案

您已经创建了 Authors 表,但是用一个小的引用了该表一个。应该是

You have created Authors table but referencing it with a small a. It should be like

references: {
  model: 'Authors',
  key: 'id'
},

这篇关于顺序迁移:关系< table>不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆