Sequelize 关联未出现在 pgAdmin 中

Posted

技术标签:

【中文标题】Sequelize 关联未出现在 pgAdmin 中【英文标题】:Sequelize Associations not showing up in pgAdmin 【发布时间】:2022-01-15 07:24:40 【问题描述】:

我正在为 Instagram 等社交媒体应用创建 restApi 端点。

有4张桌子

    用户 发布 点赞 后媒体

协会... 一个用户有很多帖子, 一个帖子属于一个用户, 一个帖子有很多帖子喜欢, 一个帖子有很多帖子媒体, 一个 postLike 属于一个帖子,属于一个用户, 一个 postMedia 属于一个帖子,

如果你仍然有点不清楚请评论任何帮助将不胜感激。

models/user.js

'use strict';
const 
  Model
 = require('sequelize');
module.exports = (sequelize, DataTypes) => 
  class User extends Model 
    /**
     * Helper method for defining associations.
     * This method is not a part of Sequelize lifecycle.
     * The `models/index` file will call this method automatically.
     */
    static associate(models) 
      // define association here????????????????????????????
      User.hasMany(models.Post, 
        foreignKey: 'user_id',
        as: 'posts',
        onDelete: 'CASCADE',
      );
      User.hasMany(models.postLikes, 
        foreignKey: 'user_id',
        as: 'postLikes',
        onDelete: 'CASCADE',
      );
    
  ;
  User.init(
    profile_img: DataTypes.STRING,
    firstname: DataTypes.STRING,
    lastName: DataTypes.STRING,
    email: DataTypes.STRING,
    phone: DataTypes.STRING,
    password: DataTypes.STRING,
    dob: DataTypes.STRING,
    gender: DataTypes.ENUM('male', 'female'),
    token: DataTypes.STRING,
    is_deleted: DataTypes.BOOLEAN,
  , 
    sequelize,
    modelName: 'User',
  );
  return User;
;

models/post.js

'use strict';
const 
  Model
 = require('sequelize');
module.exports = (sequelize, DataTypes) => 
  class Post extends Model 
    /**
     * Helper method for defining associations.
     * This method is not a part of Sequelize lifecycle.
     * The `models/index` file will call this method automatically.
     */
    static associate(models) 
      // define association here????????????????????????
      Post.hasMany(models.postLikes, 
        foreignKey: 'post_id',
        as: 'postLikes',
        onDelete: 'CASCADE',
      );
      Post.hasMany(models.postMendia, 
        foreignKey: 'post_id',
        as: 'postMedia',
        onDelete: 'CASCADE',
      )
    
  ;
  Post.init(
    description: DataTypes.STRING,
    user_id: DataTypes.INTEGER,
    is_deleted: DataTypes.BOOLEAN,
  , 
    sequelize,
    modelName: 'Post',
  );
  return Post;
;

models/postLikes.js

'use strict';
const 
  Model
 = require('sequelize');
module.exports = (sequelize, DataTypes) => 
  class postLikes extends Model 
    /**
     * Helper method for defining associations.
     * This method is not a part of Sequelize lifecycle.
     * The `models/index` file will call this method automatically.
     */
    static associate(models) 
      // define association here????????????????????
      postLikes.belongsTo(models.Post, 
        foreignKey: 'post_id',
        as: 'post',
      );
      postLikes.belongsTo(models.User, 
        foreignKey: 'user_id',
        as: 'user',
      )
    
  ;
  postLikes.init(
    post_id: DataTypes.INTEGER,
    user_id: DataTypes.INTEGER
  , 
    sequelize,
    modelName: 'postLikes',
  );
  return postLikes;
;

models/postMedia.js

'use strict';
const 
  Model
 = require('sequelize');
module.exports = (sequelize, DataTypes) => 
  class postMedia extends Model 
    /**
     * Helper method for defining associations.
     * This method is not a part of Sequelize lifecycle.
     * The `models/index` file will call this method automatically.
     */
    static associate(models) 
      // define association here????????????????????????????
      postMedia.belongsTo(models.Post, 
        foreignKey: 'post_id',
        as: 'post'
      )
    
  ;
  postMedia.init(
    url: DataTypes.STRING,
    post_id: DataTypes.INTEGER,
    type: DataTypes.ENUM('image', 'video')
  , 
    sequelize,
    modelName: 'postMedia',
  );
  return postMedia;
;

在上述所有模型中创建关联后,我正在运行命令 sequelize db:migrate

但是当我打开 pgAdmin4 控制台时,我看不到任何外键。

附注我指的是这篇文章https://dev.to/nedsoft/getting-started-with-sequelize-and-postgres-emp

【问题讨论】:

【参考方案1】:

在上面给出的博客中,家伙从未告诉我们必须添加

      references: 
          model: 
            tableName: 'Users',
            schema: 'public'
          ,

迁移内部....

参考这个页面... 只需 CTRL+F 在页面上输入外键,你就会到达那里。 Sequelize DOCS

为此浪费了 4 个多小时

【讨论】:

以上是关于Sequelize 关联未出现在 pgAdmin 中的主要内容,如果未能解决你的问题,请参考以下文章

Sequelize:模型未关联到 Model2

Sequelize - 如何从一个表中获取未被另一表关联的条目

Mean Stack -> Sequelize Mysql 关联

Sequelize:“模型 A 与模型 B 没有关联”

Sequelize-test-helpers 和 calledWith AssertionError 未定义参数

Sequelize:在关联中排序