Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include doesn't seem to work with TediousJS and MSSQL #17109

Open
3 of 6 tasks
Miggets7 opened this issue Feb 19, 2024 · 1 comment
Open
3 of 6 tasks

Include doesn't seem to work with TediousJS and MSSQL #17109

Miggets7 opened this issue Feb 19, 2024 · 1 comment
Labels
pending-approval Bug reports that have not been verified yet, or feature requests that have not been accepted yet type: bug

Comments

@Miggets7
Copy link

Issue Creation Checklist

  • I understand that my issue will be automatically closed if I don't fill in the requested information
  • I have read the contribution guidelines

Bug Description

I'm adding objects into a SQL Server database with Sequelize.
I perform all operations in one single transaction. When I want to retrieve the inserted objects, the associated models aren't fetched. But if I query them separately, they are found.

Reproducible Example

Here is the link to the SSCCE for this issue:

This code works:

 const model = await SequelizeModel.findOne({
    where: {
      name: step.relation.model,
    },
    transaction,
  });
  const modelField = await SequelizeModelField.findOne({
    where: {
      name: step.relation.field,
    },
    transaction,
  });

  const referenceModel = await SequelizeModel.findOne({
    where: {
      name: step.relation.referenceModel,
    },
    transaction,
  });

  const referenceModelField = await SequelizeModelField.findOne({
    where: {
      name: step.relation.referenceField,
    },
    transaction,
  });

But this code doesn't return the include:

const modelField = await SequelizeModelField.findOne({
    where: {
      name: step.relation.field,
    },
    include: [
      {
        model: SequelizeModel,
        where: {
          name: step.relation.model,
        },
      },
    ],
    transaction,
  });

  const referenceModelField = await SequelizeModelField.findOne({
    where: {
      name: step.relation.referenceField,
    },
    include: [
      {
        model: SequelizeModel,
        where: {
          name: step.relation.referenceModel,
        },
      },
    ],
    transaction,
  });

Generated SQL of the two selects:

SELECT
    [SequelizeModelField].[id],
    [SequelizeModelField].[name],
    [SequelizeModelField].[type],
    [SequelizeModelField].[length],
    [SequelizeModelField].[searchable],
    [SequelizeModelField].[default],
    [SequelizeModelField].[nullable],
    [SequelizeModelField].[unique],
    [SequelizeModelField].[primary],
    [SequelizeModelField].[autoIncrement],
    [SequelizeModelField].[tokenField],
    [SequelizeModelField].[encryption],
    [SequelizeModelField].[modelId],
    [SequelizeModelField].[created_at],
    [SequelizeModelField].[updated_at],
    [model].[id] AS [_0],
    [model].[name] AS [_1],
    [model].[tableName] AS [_2],
    [model].[ignoreFields] AS [_3],
    [model].[auditEnabled] AS [_4],
    [model].[created_at] AS [_5],
    [model].[updated_at] AS [_6]
FROM
    [__model_fields] AS [SequelizeModelField]
    INNER JOIN [__models] AS [model] ON [SequelizeModelField].[modelId] = [model].[id]
    AND [model].[name] = N'endpoints'
WHERE
    [SequelizeModelField].[name] = N'appconfig_id'
ORDER BY
    [SequelizeModelField].[id] OFFSET 0 ROWS FETCH NEXT 1 ROWS ONLY;

and:

SELECT
    [SequelizeModelField].[id],
    [SequelizeModelField].[name],
    [SequelizeModelField].[type],
    [SequelizeModelField].[length],
    [SequelizeModelField].[searchable],
    [SequelizeModelField].[default],
    [SequelizeModelField].[nullable],
    [SequelizeModelField].[unique],
    [SequelizeModelField].[primary],
    [SequelizeModelField].[autoIncrement],
    [SequelizeModelField].[tokenField],
    [SequelizeModelField].[encryption],
    [SequelizeModelField].[modelId],
    [SequelizeModelField].[created_at],
    [SequelizeModelField].[updated_at],
    [model].[id] AS [_0],
    [model].[name] AS [_1],
    [model].[tableName] AS [_2],
    [model].[ignoreFields] AS [_3],
    [model].[auditEnabled] AS [_4],
    [model].[created_at] AS [_5],
    [model].[updated_at] AS [_6]
FROM
    [__model_fields] AS [SequelizeModelField]
    INNER JOIN [__models] AS [model] ON [SequelizeModelField].[modelId] = [model].[id]
    AND [model].[name] = N'appconfig'
WHERE
    [SequelizeModelField].[name] = N'id'
ORDER BY
    [SequelizeModelField].[id] OFFSET 0 ROWS FETCH NEXT 1 ROWS ONLY;

Output from modelField.get({ plain: true }):

{
  id: 20,
  name: "appconfig_id",
  type: "INT",
  length: null,
  searchable: null,
  default: null,
  nullable: null,
  unique: null,
  primary: null,
  autoIncrement: null,
  tokenField: null,
  encryption: false,
  modelId: 2,
  created_at: "2024-02-17T11:14:40.807Z",
  updated_at: "2024-02-17T11:14:40.807Z",
}

The associations are defined like this:
SequelizeModel

@HasMany(() => SequelizeModelField, {
  onDelete: "CASCADE",
})
fields: SequelizeModelField[];

And SequelizeModelField:

@ForeignKey(() => SequelizeModel)
@Column
modelId: number;

@BelongsTo(() => SequelizeModel)
model: SequelizeModel;

What do you expect to happen?

I expect the include attributes to be present

What is actually happening?

The included attributes aren't present

Environment

  • Sequelize version: 6.37.0
  • Node.js version: 20.11.0
  • If TypeScript related: TypeScript version: 4.7.4
  • Database & Version: SQL Server 2019
  • Connector library & Version: TediousJS 16.7.0

Would you be willing to resolve this issue by submitting a Pull Request?

  • Yes, I have the time and I know how to start.
  • Yes, I have the time but I will need guidance.
  • No, I don't have the time, but my company or I are supporting Sequelize through donations on OpenCollective.
  • No, I don't have the time, and I understand that I will need to wait until someone from the community or maintainers is interested in resolving my issue.

Indicate your interest in the resolution of this issue by adding the 👍 reaction. Comments such as "+1" will be removed.

@Miggets7 Miggets7 added pending-approval Bug reports that have not been verified yet, or feature requests that have not been accepted yet type: bug labels Feb 19, 2024
@Miggets7
Copy link
Author

I just discovered this is the case when using minifyAliases: true. Does it make sense minifyAliases can't be used with mssql?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pending-approval Bug reports that have not been verified yet, or feature requests that have not been accepted yet type: bug
Projects
None yet
Development

No branches or pull requests

1 participant