Sequelize-Sqlite-关联问题

时间:2018-12-03 14:57:24

标签: node.js join orm sqlite sequelize.js

(对不起,我的英语)

大家好,

我创建了一个带有sequelize的项目,并且我对模型的关联有疑问。

我有以下模型(从现有的带有sequelize-auto的sqlite数据库创建):

//Genders.js
module.exports = function(sequelize, DataTypes) {
    return sequelize.define('Genders', {
        id: {
            type: DataTypes.INTEGER,
            allowNull: true,
            primaryKey: true
        },
        Type: {
            type: DataTypes.TEXT,
            allowNull: true
        }
    }, {
        freezeTableName: true,
        tableName: 'Genders'
    });
};

//Cities.js
module.exports = function(sequelize, DataTypes) {
    return sequelize.define('Cities', {
        id: {
            type: DataTypes.INTEGER,
            allowNull: true,
            primaryKey: true
        },
        ExternalId: {
            type: DataTypes.TEXT,
            allowNull: true
        },
        Name: {
            type: DataTypes.TEXT,
            allowNull: true
        },
        Province: {
            type: DataTypes.TEXT,
            allowNull: true
        },
        Description: {
            type: DataTypes.TEXT,
            allowNull: true
        }
    }, {
        freezeTableName: true,
        tableName: 'Cities'
    });
};

//Persons.js
module.exports = function(sequelize, DataTypes) {
    return sequelize.define('Persons', {
        id: {
            type: DataTypes.INTEGER,
            allowNull: true,
            primaryKey: true
        },
        Name: {
            type: DataTypes.TEXT,
            allowNull: true
        },
        Surname: {
            type: DataTypes.TEXT,
            allowNull: true
        },
        BirthCity: {
            type: DataTypes.INTEGER,
            allowNull: true,
            references: {
                model: 'Cities',
                key: 'id'
            }
        },
        Gender: {
            type: DataTypes.INTEGER,
            allowNull: true,
            references: {
                model: 'Genders',
                key: 'id'
            }
        },
        ResidenceCity: {
            type: DataTypes.INTEGER,
            allowNull: true,
            references: {
                model: 'Cities',
                key: 'id'
            }
        },
    }, {
        freezeTableName: true,
        tableName: 'Persons'
    });
};

然后,在身份验证之后,我使用以下代码导入模块:

Persons = db.sequelize.import(__dirname+"/models/Persons.js");
Cities = db.sequelize.import(__dirname+"/models/Cities.js");
Genders = db.sequelize.import(__dirname+"/models/Genders.js");

并设置关联:

Persons.hasMany(Cities, {as: 'BirthCityField', foreignKey: 'BirthCity', sourceKey: 'id'});
Persons.hasMany(Cities, {as: 'ResidenceCityField',foreignKey: 'ResidenceCity', sourceKey: 'id'});
Persons.hasMany(Genders, {foreignKey: 'Gender', sourceKey: 'id'});
Genders.belongsTo(Persons, {foreignKey: 'Gender', targetKey: 'id'});
Cities.belongsTo(Houseds, {foreignKey: 'BirthCity', targetKey: 'id'});
Cities.belongsTo(Houseds, {foreignKey: 'ResidenceCity', targetKey: 'id'});

然后我创建了此功能:

function getPersonBySurname(search) {
    return  Persons.findAll({
        where : {
            Surname : {
                [Op.like] : search+'%'
            }
        },
        include : [Genders]
    });
}

当我使用例如search ='fr'调用该函数时,什么也不返回,日志为:

SELECT `Persons`.`id`, 
`Persons`.`Name`, 
`Persons`.`Surname`, 
`Persons`.`BirthDate`, 
`Persons`.`BirthCity`, 
`Persons`.`Gender`, 
`Persons`.`Nationality`, 
`Persons`.`ResidenceCity`, 
`Persons`.`Address`, 
`Persons`.`DocumentType`, 
`Persons`.`DocumentNumber`, 
`Persons`.`FiscalCode`, 
`Persons`.`createdAt`, 
`Persons`.`updatedAt`, 
`Persons`.`OwnerHoused`, 
`Persons`.`ComponentHoused`, 
`Genders`.`id` AS `Genders.id`, 
`Genders`.`Type` AS `Genders.Type`, 
`Genders`.`createdAt` AS `Genders.createdAt`, 
`Genders`.`updatedAt` AS `Genders.updatedAt`, 
`Genders`.`Gender` AS `Genders.Gender` 
FROM `Persons` AS `Persons` 
LEFT OUTER JOIN `Genders` AS `Genders` ON `Persons`.`id` = `Genders`.`Gender` 
WHERE `Persons`.`Surname` LIKE 'fr%';

我注意到'join'被颠倒了。 确实,“加入”必须是

LEFT OUTER JOIN `Genders` AS `Genders` ON `Persons`.`Gender` = `Genders`.`id`

知道我在做什么错吗?

如果我解决了这个问题,我的下一个问题是: 我在Persons 2外键中具有相同的参考表(城市), 在这种情况下,调用一个包含就足够了吗? 例如:

function getPersonBySurname(search) {
    return  Persons.findAll({
        where : {
            Surname : {
                [Op.like] : search+'%'
            }
        },
        include : [Genders, Cities]
    });
}

谢谢

Jempus

1 个答案:

答案 0 :(得分:0)

您的代码看起来不错

我认为您可能会缺少then,因为Model.findAll返回了Promise

getPersonBySurname().then(data =>{
    console.log(data); //<----- Check here
});

因此它将控制台注销[]array of data

作为第二个问题的答案, DO READ