【问题标题】:How to tell sequelize what are the original column names to use when forming Joins (associations)?如何告诉 sequelize 在形成联接(关联)时使用哪些原始列名?
【发布时间】:2016-05-21 09:08:34
【问题描述】:

在定义关联时,我不知道如何告诉 sequelize,我的外键(以及“父”表中的主键,如果需要)的名称是什么。

我正在使用多年前存在的 mssql (2008 r2) 数据库,我无法更改该数据库中的任何内容。

这是我的模型:

CuentasBancarias_sql = sequelize.define('cuentasBancarias_sql', {
    id: { type: Sequelize.INTEGER, field: 'CuentaInterna', allowNull: false, autoIncrement: true, primaryKey: true },
}, {
     tableName: 'CuentasBancarias'
});

Chequeras_sql = sequelize.define('chequeras_sql', {
    id: { type: Sequelize.INTEGER, field: 'NumeroChequera', allowNull: false, autoIncrement: true, primaryKey: true },
    numeroCuenta: { type: Sequelize.INTEGER, field: 'NumeroCuenta', allowNull: false },
}, {
     tableName: 'Chequeras'
});


MovimientosBancarios_sql = sequelize.define('movimientosBancarios_sql', {
    id: { type: Sequelize.INTEGER, field: 'ClaveUnica', primaryKey: true, autoIncrement: true, allowNull: false },
    claveUnicaChequera: { type: Sequelize.INTEGER, field: 'ClaveUnicaChequera', allowNull: false },
}, {
     tableName: 'MovimientosBancarios'
});

它们只是 3 个一对多表,具有主键和外键。这是一个非常正常、简单和常见的模型。

现在,这些是我的联想:

CuentasBancarias_sql.hasMany(Chequeras_sql, { foreignKey: 'NumeroCuenta'} );
Chequeras_sql.belongsTo(CuentasBancarias_sql, { foreignKey: 'NumeroCuenta' } );
Chequeras_sql.hasMany(MovimientosBancarios_sql, { foreignKey: 'ClaveUnicaChequera' } );
MovimientosBancarios_sql.belongsTo(Chequeras_sql, { foreignKey: 'ClaveUnicaChequera' } );

似乎一切正常。但是,当我添加关联时,findAll 会失败,因为它会在 sql select 语句的 Join 中生成错误的列名。

所以,我想我的问题是:如果我有一个 sql server 数据库并且我无法更改任何内容,我如何告诉 sequelize 原始(数据库)列名(pk 和 fk),以便 sequelize 可以构造正确的 Joins ,当做查询(findAll)?

这就是我编写查询的方式:

MovimientosBancarios_sql.findAndCountAll({
                where: { ... },
                include: [
                    { model: Chequeras_sql, include: [ { model: CuentasBancarias_sql }] },
                ],
            })

非常感谢您的帮助,再见...

【问题讨论】:

  • 列名怎么错了? IE。实际生成的sql是什么,你要的sql是什么?
  • 您好 Jan,感谢您的及时回复。我做了一些纠正这个问题的事情。但是,我只是不知道具体是什么。我在下面添加更多信息以进行详细说明。再次感谢...

标签: sequelize.js


【解决方案1】:

正如我在上面的评论中所说,现在一切正常。问题已修复。 这就是我设置关系的方式:

CuentasBancarias_sql.hasMany(Chequeras_sql, { foreignKey: 'NumeroCuenta'} );
Chequeras_sql.belongsTo(CuentasBancarias_sql, { foreignKey: 'NumeroCuenta' } );
Chequeras_sql.hasMany(MovimientosBancarios_sql, { foreignKey: 'ClaveUnicaChequera' } );
MovimientosBancarios_sql.belongsTo(Chequeras_sql, { foreignKey: 'ClaveUnicaChequera' } );

Sequelize 正确地从 foreingKey 子句中获取列名来形成 Joins in Select 语句。这就是 Join 部分的结束方式:

FROM [MovimientosBancarios] AS [movimientosBancarios_sql] LEFT OUTER JOIN [Chequeras] AS [chequeras_sql] 
ON [movimientosBancarios_sql].[ClaveUnicaChequera] = [chequeras_sql].[NumeroChequera] 
INNER JOIN [CuentasBancarias] AS [chequeras_sql.cuentasBancarias_sql] 
ON [chequeras_sql].[NumeroCuenta] = [chequeras_sql.cuentasBancarias_sql].[CuentaInterna] AND [chequeras_sql.cuentasBancarias_sql].[Cia] = 37 

注意 Inner JoinCia = 30 部分,因为下面我的查询中的 where 部分:

MovimientosBancarios_sql.findAndCountAll({
                where: filter,
                include: [
                    { model: Chequeras_sql, include: [ { model: CuentasBancarias_sql, where: { Cia: compania.numero } }] },
                ],
            })

再次感谢,再见...

【讨论】:

    猜你喜欢
    • 2013-10-22
    • 2020-05-22
    • 2017-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-08
    • 2017-08-18
    相关资源
    最近更新 更多