Skip to content

feat: forbid using attr names that are ambiguous with $nested.syntax$ - #13992

Merged
ephys merged 8 commits into
mainfrom
ephys/restrict-nested-syntax-attr
Feb 11, 2022
Merged

ephys merged 8 commits into
mainfrom
ephys/restrict-nested-syntax-attr

Conversation

@ephys

@ephys ephys commented Jan 22, 2022 •

Copy link
Copy Markdown
Member

Pull Request Checklist

  • Have you added new tests to prevent regressions?
  • Does npm run test or npm run test-DIALECT pass with this change (including linting)?
  • Is a documentation update included (if this change modifies existing APIs, or introduces new ones)?
  • [n/a] Did you update the typescript typings accordingly (if applicable)?
  • [n/a] Does the description below contain a link to an existing issue (Closes #[issue]) or a description of the issue you are solving?
  • Did you follow the commit message conventions explained in CONTRIBUTING.md?

Description Of Change

v7 only

It's currently possible to name an attribute $name$ or $na.me$. This is ambiguous with the $nested.syntax$ that can be used in queries.

This PR makes that syntax reserved and rejects any attribute named in a way that would be ambiguous with the nested attribute reference syntax.

It's opinionated as I found a couple places in the code where Sequelize tries to make it work, but there are many, many other places where it just silently breaks.

This is a breaking change, and should be marked as such when squashing the commits from this PR.

@ephys

ephys commented Jan 22, 2022 •

Copy link
Copy Markdown
Member Author

I think we should forbid using . in attribute names too, as that would be ambiguous with the json.reference.syntax and the $nested.attribute.reference.syntax$

here is a convoluted example of things breaking because an attribute name included a dot:

const foo = sequelize.define('foo', {
  'my.attribute': DataTypes.TEXT,
});

const my = sequelize.define('my', { attribute: DataTypes.TEXT });

foo.belongsTo(my);

await sequelize.sync({ force: true });

await foo.findAll({
  include: [my],
  where: {
    '$my.attribute$': 'value' // will this reference foo['my.attribute'] or foo.my.attribute?
  },
  logging: console.log,
});

/*
SELECT "foo"."id", 
 "my.attribute", 
 "foo"."myId", 
 "my"."id" AS "my.id", 
 "my"."attribute" AS "my.attribute" // conflict here too!
FROM "foos" AS "foo" 
LEFT OUTER JOIN "mies"  AS "my" 
 ON "foo"."myId" = "my"."id" 
WHERE "my"."attribute" = 'value';
*/

Much safer to just ban doing this.

Edit: I've included that change in this PR too.

@ephys ephys changed the title fix: forbid using attr names that are ambiguous with $nested.syntax$ feat: forbid using attr names that are ambiguous with $nested.syntax$ Jan 23, 2022
@ephys ephys self-assigned this Jan 23, 2022
@ephys
ephys requested a review from a team January 25, 2022 13:58
have been removed and all typings are now directly retrieved from the actual TypeScript code.
You'll likely find many tiny differences which however should be easy to fix.

## Attributes cannot start and end with `$`, or include `.`

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe good to add an example of how people can change the names of the attributes they are using?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done! :)

@ephys
ephys requested a review from WikiRik February 6, 2022 12:01
@sdepold

sdepold commented Feb 6, 2022

Copy link
Copy Markdown
Member

Can we add this to the list of breaking changes in the migration docs?

@ephys

ephys commented Feb 6, 2022

Copy link
Copy Markdown
Member Author

It is :)

@sdepold

sdepold commented Feb 11, 2022

Copy link
Copy Markdown
Member

Ah true :) Guess I didn't see it on my phone. Let's get this into main then!

@ephys
ephys merged commit b3c2270 into main Feb 11, 2022
@ephys
ephys deleted the ephys/restrict-nested-syntax-attr branch February 11, 2022 12:24
@ephys ephys added the breaking change For issues and PRs. Changes that break compatibility and require a major version increment. label Feb 11, 2022
@github-actions

Copy link
Copy Markdown
Contributor

🎉 This PR is included in version 7.0.0-alpha.9 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

breaking change For issues and PRs. Changes that break compatibility and require a major version increment. released on @v7

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants