相关疑难解决方法(0)

Node.js Sequelize UUID主键+ Postgres

我尝试通过使用sequelize创建数据库模型,但是我面临模型主键的问题。

设置

我在Docker容器中使用Postgres(v10),并为模型隔离(Node.js v10.1.0)和为请求处理的GraphQL(0.13.2)+ GraphQL-Sequalize(8.1.0)。

问题

通过sequelize-cli创建模型后,我手动尝试用uuid替换id列。这是我正在使用的模型迁移。

'use strict';
const DataTypes = require('sequelize').DataTypes;

module.exports = {
  up: (queryInterface, Sequelize) => {
    return queryInterface.createTable('Currencies', {
      uuid: {
        primaryKey: true,
        type: Sequelize.UUID,
        defaultValue: DataTypes.UUIDV4,
        allowNull: false
      },
      name: {
        type: Sequelize.STRING
      },
      ticker: {
        type: Sequelize.STRING
      },
      alt_tickers: {
        type: Sequelize.ARRAY(Sequelize.STRING)
      },
      createdAt: {
        allowNull: false,
        type: Sequelize.DATE
      },
      updatedAt: {
        allowNull: false,
        type: Sequelize.DATE
      }
    });
  },
  down: (queryInterface, Sequelize) => {
    return queryInterface.dropTable('Currencies');
  }
};
Run Code Online (Sandbox Code Playgroud)

模型:

'use strict'; …
Run Code Online (Sandbox Code Playgroud)

postgresql orm node.js sequelize.js graphql

2
推荐指数
2
解决办法
4059
查看次数

标签 统计

graphql ×1

node.js ×1

orm ×1

postgresql ×1

sequelize.js ×1