【问题标题】:Sequelize association generate fields that do no existsSequelize 关联生成不存在的字段
【发布时间】:2019-08-07 00:56:40
【问题描述】:

我正在将 PHP API 转换为 GraphQL api。我使用 Sequelize 作为 ORM 包。我有两个表,我想通过 hasOne 连接进行耦合。

这是我的咨询服务模型:


module.exports = function(sequelize, DataTypes) {
    const AdvisoryService = sequelize.define(
        'advisoryService',
        {
            id: {
                type: DataTypes.INTEGER(11),
                allowNull: false,
                primaryKey: true,
                autoIncrement: true,
                field: 'id',
            },
            country: {
                type: DataTypes.INTEGER(11),
                allowNull: true,
                references: {
                    model: 'system_country',
                    key: 'id',
                },
                field: 'country',
            },
          //REDACTED
        },
        {
            tableName: 'advisory_service',
            timestamps: false,
        },
    )
    AdvisoryService.associate = models => {
        AdvisoryService.hasOne(models.systemCountry, {
            as: 'Country',
            foreignKey: 'country',
        })
    }
    return AdvisoryService
}

还有我的 systemCountry 模型:

/* jshint indent: 2 */

module.exports = function(sequelize, DataTypes) {
    return sequelize.define(
        'systemCountry',
        {
            id: {
                type: DataTypes.INTEGER(11),
                allowNull: false,
                primaryKey: true,
                autoIncrement: true,
                field: 'id',
            },
            oldId: {
                type: DataTypes.INTEGER(11),
                allowNull: true,
                unique: true,
                field: 'old_id',
            },
            subcontinentId: {
                type: DataTypes.INTEGER(11),
                allowNull: true,
                references: {
                    model: 'system_subcontinent',
                    key: 'id',
                },
                field: 'subcontinent_id',
            },
            code: {
                type: DataTypes.STRING(2),
                allowNull: false,
                field: 'code',
            },
            longCode: {
                type: DataTypes.STRING(3),
                allowNull: false,
                field: 'long_code',
            },
            currency: {
                type: DataTypes.STRING(255),
                allowNull: true,
                field: 'currency',
            },
            currencyCode: {
                type: DataTypes.STRING(255),
                allowNull: true,
                field: 'currency_code',
            },
            isEu: {
                type: DataTypes.INTEGER(1),
                allowNull: false,
                field: 'is_eu',
            },
            isAsean: {
                type: DataTypes.INTEGER(1),
                allowNull: false,
                field: 'is_asean',
            },
            isFragileWb: {
                type: DataTypes.INTEGER(1),
                allowNull: false,
                field: 'is_fragile_wb',
            },
            isFragilePf: {
                type: DataTypes.INTEGER(1),
                allowNull: false,
                field: 'is_fragile_pf',
            },
            isFragileOecd: {
                type: DataTypes.INTEGER(1),
                allowNull: false,
                field: 'is_fragile_oecd',
            },
            title: {
                type: DataTypes.STRING(255),
                allowNull: false,
                field: 'title',
            },
            latitude: {
                type: 'DOUBLE',
                allowNull: true,
                field: 'latitude',
            },
            longitude: {
                type: 'DOUBLE',
                allowNull: true,
                field: 'longitude',
            },
        },
        {
            tableName: 'system_country',
            timestamps: false,
        },
    )
}

它会生成以下查询:

Executing (default): SELECT `id`, `old_id` AS `oldId`, `subcontinent_id` AS `subcontinentId`, `code`, `long_code` AS `longCode`, `currency`, `currency_code` AS `currencyCode`, `is_eu` AS `isEu`, `is_asean` AS `isAsean`, `is_fragile_wb` AS `isFragileWb`, `is_fragile_pf` AS `isFragilePf`, `is_fragile_oecd` AS `isFragileOecd`, `title`, `latitude`, `longitude`, `country` FROM `system_country` AS `systemCountry` WHERE `systemCountry`.`country` = 1 LIMIT 1;

并抛出以下错误:SequelizeDatabaseError: Unknown column 'country' in 'field list'。我得到了,因为 system_country 表中没有县字段。我不知道为什么协会会生成国家字段。有人能指出我做错了什么吗?

【问题讨论】:

    标签: node.js graphql sequelize.js


    【解决方案1】:

    你的关系 -

    AdvisoryService.associate = models => {
        AdvisoryService.hasOne(models.systemCountry, {
            as: 'Country',
            foreignKey: 'country',
        })
    }
    

    正在定义与关键国家的关系,因此它在 systemCountry 表中找到 country

    在您的关系定义中使用以下对象 -

    {as: "Country", foreignKey: "OtherTableColumn", sourceKey: "SameTableColumn"}
    

    【讨论】:

      猜你喜欢
      • 2021-04-21
      • 1970-01-01
      • 1970-01-01
      • 2015-05-26
      • 1970-01-01
      • 2021-12-27
      • 2019-08-18
      • 2019-05-01
      • 2018-06-14
      相关资源
      最近更新 更多