Skip to content

feat: support UPDATE ... FROM ... syntax#833

Merged
tyt2y3 merged 5 commits intoSeaQL:update-fromfrom
the-wondersmith:feat/update-from
Feb 15, 2025
Merged

feat: support UPDATE ... FROM ... syntax#833
tyt2y3 merged 5 commits intoSeaQL:update-fromfrom
the-wondersmith:feat/update-from

Conversation

@the-wondersmith
Copy link
Copy Markdown
Contributor

PR Info

  • Partially addresses #608

New Features

  • Adds support for explicitly specifying a FROM clause in an UPDATE query
    • e.x. UPDATE table1 SET some_column = table2.another_column FROM table2 WHERE table1.another_column = table2.a_different_column

@the-wondersmith
Copy link
Copy Markdown
Contributor Author

Just tagging @tyt2y3 @billy1624 (as the most active/recent mergers) to try and get some traction on this 😅

@tyt2y3
Copy link
Copy Markdown
Member

tyt2y3 commented Dec 1, 2024

Thank you for the PR! sadly UPDATE ... FROM is not valid syntax on MySQL.
I think it is also supported in SQLite, so it's worth adding, but may be we need to indicate this in the docs

@the-wondersmith
Copy link
Copy Markdown
Contributor Author

the-wondersmith commented Dec 1, 2024

Thank you for the PR! sadly UPDATE ... FROM is not valid syntax on MySQL. I think it is also supported in SQLite, so it's worth adding, but may be we need to indicate this in the docs

Totally valid. We can restrict it by feature flag, if that works? So it's only available when the proper feature flags are enabled, and the emitted queries are syntactically correct for the enabled feature-set.

Out of curiosity, do you happen to know the equivalent syntax for MySQL? Once-upon-a-time I was extremely familiar with the major flavors of SQL syntax, as I was doing a lot of tinkering in the internals of SQLAlchemy due to being the maintainer for a couple of esoteric dialects for that package. In the intervening time, I've transitioned to rust almost completely, so tl;dr SeaORM/SeaQuery are the gotos AFAIC 😅

Anyway, if you're amenable to the update, I'll sort out the syntax / engine pairings and update the PR.

///
/// assert_eq!(
/// query.to_string(MysqlQueryBuilder),
/// "UPDATE `glyph` SET `tokens` = `character`.`character` FROM `character` WHERE `glyph`.`image` = `character`.`user_data`"
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

The syntax of MySQL appears similar, but not identical. Here a MySQL UPDATE JOIN could be used to achieve the same:

UPDATE `glyph`
JOIN `character`
  ON `glyph`.`image` = `character`.`user_data`
SET `glyph`.`tokens` = `character`.`character`;

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@gronke Took a wild swing at at least getting started here, and extracted the specific behavior into a discrete method (prepare_update_from_table_refs) so it can be overridden as needed by whichever dialect. I've got the MySQL-specific handling as a unimplemented! call at the moment though, cause I've got a case of end-of-year-holidays-brain.

If you have a second, would you take a look at the changes and let me know if there's a better / more preferable way of doing this please let me know? Otherwise I'll carry on as time allows 😁

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@gronke ^bump

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@gronke ^bump 🙂

@just-in-chang
Copy link
Copy Markdown
Contributor

Would love to see this landed! This would provide support for stuff like using SeaQuery to construct queries for batch update operations that utilize CTEs and value tuples.

@the-wondersmith
Copy link
Copy Markdown
Contributor Author

Would love to see this landed! This would provide support for stuff like using SeaQuery to construct queries for batch update operations that utilize CTEs and value tuples.

I'd love to get it landed 😅. I see you've got a Contributor tag next to your name here. Any chance you could poke @gronke for some feedback here?

@tyt2y3 tyt2y3 changed the base branch from master to update-from February 15, 2025 18:09
@tyt2y3 tyt2y3 merged commit 1e1542a into SeaQL:update-from Feb 15, 2025
@tyt2y3
Copy link
Copy Markdown
Member

tyt2y3 commented Feb 15, 2025

I will give it a try

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants