如何在Sequelize.js ORM语法中指定此复杂的SQL查询?

时间:2016-05-23 12:17:38

标签: sequelize.js

我不知道如何编写一个应该像这样输出SQL的Sequelize查询:

select 
    pe.*, 
    cd.*
from
    people as pe
    inner join parties as pa on pa.people_id = pe.people_id
    inner join contact_details as cd on cd.parties_id = pa.parties_id
    inner join contact_details_types cdt on cdt.contact_details_types_id = cd.contact_details_types_id
where
    cd.value ='emailaddress'
    and cdt.contact_details_types_id in ('132', '546')

我的数据库表看起来像这样,下面的表有上面表格的ID:

enter image description here

Sequelize documentation没有复杂的例子。所以这是我的尝试:

getPeopleWithWorkOrPersonalEmail(email: string): Promise<[IDatabaseModel]> 
{
    return this._db.getEntities()
        .people
        .findAll(
        {
            include:
            [
                { model: this._db.getEntities().parties },
                {
                    model: this._db.getEntities().contact_details,
                    where: {value: email},
                    include:
                    [{
                        model: this._db.getEntities().contact_details_types,
                        where:
                        {
                            contact_details_types_id:
                            {
                                $in : ['132','546']
                            }
                        }
                    }]
                }
            ]
        });
}

这会出错:dbo.parties is not associated to dbo.people!

这是真的,但我不确定如何做到这一点。

更新

如果有帮助,以下是我模型的相关部分:

people.ts

getModel(db : ISequelize) : IDatabaseModel 
{
    const options = helper.getDatabaseDefaultOptions('people');
    const entity : IObjectWithStringKey = {};
    entity['people_id'] = {type: Sequelize.UUID, primaryKey: true, allowNull : false};
    entity['surnames'] = Sequelize.TEXT;
    const result = db.define('dbo.people', entity, options);
    Model._model = result;
    return result;
}

parties.ts

getModel(db : ISequelize) : IDatabaseModel {
    const options = helper.getDatabaseDefaultOptions('parties');
    const entity : IObjectWithStringKey = {};
    entity['parties_id'] = {type: Sequelize.UUID, primaryKey: true, allowNull : false};
    entity['people_id'] = helper.getForeignKey('people_id', people.getModel());
    entity['companies_id'] = helper.getForeignKey('companies_id', companies.getModel());
    const result = db.define('dbo.parties', entity, options);
    result.belongsTo(people.getModel(), {foreignKey: 'people_id'});
    result.belongsTo(companies.getModel(), {foreignKey: 'companies_id'});
    Model._model = result;
    return result;
}

contact_details.ts

getModel(db : ISequelize) : IDatabaseModel {
    if (Model._model)
        return Model._model;
    const options = helper.getDatabaseDefaultOptions('contact_details');
    const entity : IObjectWithStringKey = {};
    entity['contact_details_id'] = {type: Sequelize.UUID, primaryKey: true, allowNull : false};
    entity['value'] = Sequelize.TEXT;
    entity['parties_id'] = helper.getForeignKey('parties_id', parties.getModel());
    const result = db.define('dbo.contact_details', entity, options);
    result.belongsTo(contactDetailsTypes.getModel(db), {foreignKey: 'contact_details_types_id'});
    result.belongsTo(parties.getModel(db), {foreignKey: 'parties_id'});
    Model._model = result;
    return result;
}

contact_details_types.ts

getModel(db : ISequelize) : IDatabaseModel {
    if (!Model._model)
        Model._model = helper.getEnumEntity(db, 'contact_details_types');
    return Model._model;
}

0 个答案:

没有答案