关联关联生成不存在的字段

时间:2019-03-15 21:57:40

标签: node.js graphql sequelize.js

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

这是我的AdvisoryService模型:


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:“字段列表”中的未知列“国家”。我得到的是因为system_country表中没有County字段。我不知道为什么该协会会产生国家/地区字段。有人可以指出我做错了什么吗?

1 个答案:

答案 0 :(得分:0)

您与您的关系-

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

正在定义与关键国家的关系,因此在 systemCountry

中找到国家/地区

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

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