首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从dynamodb (dynamoose)迁移到postgresql (sequelize)-设置hashKey

从dynamodb (dynamoose)迁移到postgresql (sequelize)-设置hashKey
EN

Stack Overflow用户
提问于 2019-04-03 12:11:32
回答 1查看 280关注 0票数 0

我已经使用dynamoose创建了一个简单的模型,现在我想使用sequelize将它迁移到postgresql中。我熟悉dynamodb以及如何在其中设置散列键和范围键,但我如何使用sequelize在postgresql中实现同样的功能?下面是我之前使用dynamoose的模型,以及我使用sequelize迁移它的努力,但我不确定如何处理这些键:

代码语言:javascript
复制
//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进行迁移:

代码语言:javascript
复制
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。谢谢。

EN

回答 1

Stack Overflow用户

发布于 2019-05-21 06:14:52

我正在用TypeORM做类似的事情,但在我看来,对于Sequelize Associations,你只需要设置外键,如下所示:

代码语言:javascript
复制
 chatId: {
    type: Sequelize.STRING,
    references: {
      model: Chat,
      key: 'id'
    }
  }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55486954

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档