首页 > 解决方案 > 在 sequilize.js 中使用对象数组时出错?

问题描述

当我试图编译这段代码时

const UserDetail = connection.define('user_detail', {
    id: {
        allowNull: false,
        primaryKey: true,
        type: Sequelize.INTEGER,
        autoIncrement: true,
    },
    name: {
        type: Sequelize.STRING,
        allowNull: false,
        len: [6, 50],

    },
    about: {
        type: Sequelize.TEXT,
        allowNull: false,
    },
    position: {
        type: Sequelize.STRING,
        allowNull: false
    },
    birth: [{
        year: {
            type: Sequelize.INTEGER,
            allowNull: false
        },
        location: {
            type: Sequelize.TEXT,
            allowNull: false
        },
    }]
});

它抛出这个错误

Unhandled rejection SequelizeDatabaseError: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '[object Object], `createdAt` DATETIME NOT NULL, `updatedAt` DATETIME NOT NULL, P' at line 1

创建对象数组的好方法是什么?我用谷歌搜索了这个问题,但找不到解决方案,而 Stack Overflow 上的上一个问题我没有得到。

标签: sequelize.js

解决方案


如果您希望它是一个“数组”,您可以使用不同的相关表,或者您可以将字段定义为:

birth_year: {
  type: Sequelize.INTEGER,
  allowNull: false,
},
birth_location: {
  type: Sequelize.TEXT,
  allowNull: false,
},

推荐阅读