Sequelize Associations 没有出现在 pgAdmin 中
Sequelize Associations not showing up in pgAdmin
我正在为 Instagram 等社交媒体应用程序创建 restApi 端点。
有4张桌子
- 用户
- Post
- post赞
- post媒体
协会...
单个用户有许多post个,
单个 post 属于一个用户,
一个post有很多post赞,
一个 post 有许多 post 个媒体,
一个post赞属于一个post,属于一个用户,
post媒体属于一个 post,
如果您还有点不清楚,请发表评论,我们将不胜感激。
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 控制台时,我没有看到任何外键。
P.S。我指的是这篇文章https://dev.to/nedsoft/getting-started-with-sequelize-and-postgres-emp
在上面给出的博客中,那个人从来没有告诉我们必须添加
references: {
model: {
tableName: 'Users',
schema: 'public'
},
迁移内部....
参考此页面...只需按 CTRL+F 选择页面上的外键,您就会到达那里。
Sequelize DOCS
为此浪费了 4 个多小时
我正在为 Instagram 等社交媒体应用程序创建 restApi 端点。
有4张桌子
- 用户
- Post
- post赞
- post媒体
协会... 单个用户有许多post个, 单个 post 属于一个用户, 一个post有很多post赞, 一个 post 有许多 post 个媒体, 一个post赞属于一个post,属于一个用户, post媒体属于一个 post,
如果您还有点不清楚,请发表评论,我们将不胜感激。
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 控制台时,我没有看到任何外键。
P.S。我指的是这篇文章https://dev.to/nedsoft/getting-started-with-sequelize-and-postgres-emp
在上面给出的博客中,那个人从来没有告诉我们必须添加
references: {
model: {
tableName: 'Users',
schema: 'public'
},
迁移内部....
参考此页面...只需按 CTRL+F 选择页面上的外键,您就会到达那里。 Sequelize DOCS
为此浪费了 4 个多小时