我已经使用dynamoose创建了一个简单的模型,现在我想使用sequelize将它迁移到postgresql中。我熟悉dynamodb以及如何在其中设置散列键和范围键,但我如何使用sequelize在postgresql中实现同样的功能?下面是我之前使用dynamoose的模型,以及我使用sequelize迁移它的努力,但我不确定如何处理这些键:
//dynamoose
var dynamoose = require("dynamoose");
var MessageRatingSchema = new dynamoose.Schema({
id: {
type: String,
index: {
global: true
}
},
chatId: {
type: String,
hashKey: true
},
ratingType: {
type: String,
enum: ["NEGATIVE", "POSITIVE", "NEUTRAL", "MIXED"],
rangeKey: true
},
rating: {
type: Number
}
});
module.exports = MessageRatingSchema;尝试使用sequelize进行迁移:
const Sequelize = require("sequelize");
const sequelize = new Sequelize('PGDATABASE', 'PGUSER', 'PGPASS', {
host: 'localhost',
dialect: 'postgres'
});
const MessageRating = sequelize.define('MessageRating', {
id: {
type: Sequelize.STRING,
primaryKey: true
},
chatId: {
type: Sequelize.STRING
//hashkey
},
ratingType: {
type: Sequelize.STRING,
enum: ["NEGATIVE", "POSITIVE", "NEUTRAL", "MIXED"]
//sortkey
},
rating:{
type: Sequelize.NUMBER
}
}, {
// options
});
module.exports = MessageRating;请告诉我这是否正确,以及如何使用sequelize设置相应的hashKeys和rangeKeys。谢谢。
发布于 2019-05-21 06:14:52
我正在用TypeORM做类似的事情,但在我看来,对于Sequelize Associations,你只需要设置外键,如下所示:
chatId: {
type: Sequelize.STRING,
references: {
model: Chat,
key: 'id'
}
}https://stackoverflow.com/questions/55486954
复制相似问题