postgresql - Sequelize 迁移 : relation <table> does not exist

标签 postgresql sequelize.js sequelize-cli

我正在研究 Author hasMany Books 示例并尝试运行 sequelize-cli 迁移,但在运行以下迁移时遇到以下问题:

ERROR: relation "authors" does not exist

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

'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');
  }
};

第二次迁移创建一本书:

'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');
  }
};

创建作者和书籍之间关系的迁移:

'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'
    )
  }
};

这些是我的模型:

作者.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;
};

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

enter image description here

我正在使用以下内容:

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

我是 Sequelize 的新手,如有任何帮助,我们将不胜感激!

最佳答案

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

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

关于postgresql - Sequelize 迁移 : relation <table> does not exist,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50950358/

相关文章:

postgresql - SELECT 语句的历史结果

postgresql - Postgres 字符串前的 "E"是什么?

python - Django 的 NotImplementedError : aggregate() + distinct(fields) not implemented

mysql - 属于或有一个 Sequelize

sequelize.js - sequelize cli 不使用 postgres

mysql - 使用 mySQL : Unable to set default value 续写 UUID

node.js - 如何通过sequelize queryInterface插入关联行

django - postgres 和 docker 组合 : change service status to ready after additional initialization

javascript - 迁移到 V5 后序列化记录问号而不是值

node.js - sequelize datatypes.NOW 函数不生成时间戳