无法使用semelize添加外键约束

发布于 2025-02-12 19:53:36 字数 1622 浏览 0 评论 0原文

尝试使用quelize创建一个待办事项列表,但我总是遇到这个错误: “也无法使用semelize添加外键约束”,即使在Stackoverflow中寻找一些线索,Somone也可以提供帮助?

module.exports = {
  async up(queryInterface, Sequelize) {
    await queryInterface.createTable('Todos', {
      id: {
        allowNull: false,
        autoIncrement: true,
        primaryKey: true,
        type: Sequelize.INTEGER,
      },
      description: {
        type: Sequelize.STRING,
        allowNull: false,
      },
      createdAt: {
        allowNull: false,
        type: Sequelize.DATE,
      },
      status_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: {
          model: 'Statuses',
          key: 'id',
        },
      },
    });
  },
  async down(queryInterface) {
    await queryInterface.dropTable('Todos');
  },
};

如果有人有任何想法,

module.exports = {
  async up(queryInterface, Sequelize) {
    await queryInterface.createTable('Statuses', {
      id: {
        allowNull: false,
        autoIncrement: true,
        primaryKey: true,
        type: Sequelize.INTEGER,
      },
      name: {
        type: Sequelize.STRING,
        allowNull: false,
      },
    });
  },
  async down(queryInterface) {
    await queryInterface.dropTable('Statuses');
  },
};

Relationship in models

// Status model
  Status.associate = (models) => {
    Status.belongsTo(models.Todo, { foreignKey: 'status_id', as: 'todo' });
  };

// Todo model
  Todo.associate = (models) => {
    Todo.hasMany(models.Status, { foreignKey: 'status_id', as: 'statuses' });
  };

我真的很感激 谢谢

Trying to create a todo list with sequelize but I always hava this error:
"Cannot add foreign key constraint using Sequelize" Even looking for some clues in StackOverflow, somone can help?

ToDo Migration

module.exports = {
  async up(queryInterface, Sequelize) {
    await queryInterface.createTable('Todos', {
      id: {
        allowNull: false,
        autoIncrement: true,
        primaryKey: true,
        type: Sequelize.INTEGER,
      },
      description: {
        type: Sequelize.STRING,
        allowNull: false,
      },
      createdAt: {
        allowNull: false,
        type: Sequelize.DATE,
      },
      status_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        references: {
          model: 'Statuses',
          key: 'id',
        },
      },
    });
  },
  async down(queryInterface) {
    await queryInterface.dropTable('Todos');
  },
};

Status migration

module.exports = {
  async up(queryInterface, Sequelize) {
    await queryInterface.createTable('Statuses', {
      id: {
        allowNull: false,
        autoIncrement: true,
        primaryKey: true,
        type: Sequelize.INTEGER,
      },
      name: {
        type: Sequelize.STRING,
        allowNull: false,
      },
    });
  },
  async down(queryInterface) {
    await queryInterface.dropTable('Statuses');
  },
};

Relationship in models

// Status model
  Status.associate = (models) => {
    Status.belongsTo(models.Todo, { foreignKey: 'status_id', as: 'todo' });
  };

// Todo model
  Todo.associate = (models) => {
    Todo.hasMany(models.Status, { foreignKey: 'status_id', as: 'statuses' });
  };

If someone have any idea I'm really appreciate! Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

雪化雨蝶 2025-02-19 19:53:36

当“ todos”表具有“ status_id”时,

它必须“属于“状态”表。

或者,您应该将“ todo_id”保存在“状态”表中。

  // Status model
  Status.associate = (models) => {
    Status.hasMany(models.Todo, { foreignKey: 'status_id', as: 'todos' });
  };

  // Todo model
  Todo.associate = (models) => {
    Todo.belongsTo(models.Status, { foreignKey: 'status_id', as: 'status' });
  };

When the "Todos" table has the "status_id",

it must "belong to" the "Statuses" table.

Or you should save the "todo_id" in the "Statuses" table.

  // Status model
  Status.associate = (models) => {
    Status.hasMany(models.Todo, { foreignKey: 'status_id', as: 'todos' });
  };

  // Todo model
  Todo.associate = (models) => {
    Todo.belongsTo(models.Status, { foreignKey: 'status_id', as: 'status' });
  };
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文