Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

请问如何在创建了模型之后,如何通过 migrate 添加关联关系? #15

Open
monkeym4ster opened this issue Sep 5, 2018 · 1 comment
Labels

Comments

@monkeym4ster
Copy link

monkeym4ster commented Sep 5, 2018

目前状态

创建了 User 模型,以及 Profile 模型。

需求

给 user 模型和 profile 模型添加一对一关系。

已有模型 的情况之下,如何能通过 sequelize-cli 的 migrate 功能实现添加关联关系的操作呢?

我想我知道如何在 model 层面里进行关联关系的添加,但是目的是 「版本管理」 嘛。

// app/model/user.js
...
User.associate = () => User.hasOne(Profile);
...

// app/model/profile.js
...
Profile.associate = () => Profile.belongsTo(User);
...

Models

// app/model/user.js

module.exports = app => {
  const { STRING} = app.Sequelize;

  const User = app.model.define('User', {
    id: {
      type: INTEGER,
      primaryKey: true,
      autoIncrement: true,
    },
    username: STRING,
    password: STRING,
  });

  return User;
};
// app/model/profile.js

module.exports = app => {
  const { STRING, INTEGER } = app.Sequelize;

  const Profile = app.model.define('Profile', {
    description: STRING,
    user_id: INTEGER,
  });

  return Profile;
};
@prettyboyweiwei
Copy link

a23f928ad797b9df09880ef06aa40e60
大佬可以告诉小弟,改了model的外键之后,生成的sql语句在哪里?怎么执行呢?怎么通过cli变更到数据库呢???
看了无数文档,始终没有找到办法。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants