You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
complex update with joins produce invalid sql syntax with sqlite and postgres.
the following test it "generates an update statement with joins" is checking an sql query that is invalid in sqlite and postgres.
# frozen_string_literal: truerequire'bundler/inline'gemfile(true)dosource'https://rubygems.org'git_source(:github){ |repo| "https://github.com/#{repo}.git"}gem'rails',github: 'rails/rails',branch: 'main'gem'sqlite3'endrequire'active_record'require'minitest/autorun'require'logger'# This connection will do for database-independent bug reports.ActiveRecord::Base.establish_connection(adapter: 'sqlite3',database: ':memory:')ActiveRecord::Base.logger=Logger.new(STDOUT)ActiveRecord::Schema.definedocreate_table:users,force: truedo |t|
t.integer:posts_countendcreate_table:posts,force: truedo |t|
t.integer:user_idendendclassBugTest < ActiveSupport::TestCasedeftest_update_with_joinsum=Arel::UpdateManager.newtable=Arel::Table.new(:users)join_table=Arel::Table.new(:posts)join_source=Arel::Nodes::JoinSource.new(table,[table.create_join(join_table)])um.tablejoin_sourceum.set[[table[:posts_count],join_table[:id].count]]assert_nothing_raised{ActiveRecord::Base.connection.update(um)}endend
Expected behavior
it should be possible to set the FROM to join other tables. and at the end generate the following sql UPDATE "users" SET "posts_count" = COUNT("posts"."id") FROM "posts" WHERE "users"."id" = "posts"."user_id".
Arel UpdateManager with a join generates invalid SQL syntax in MySQL also, I believe. @ignacio-chiazzo may have insight here, having worked on this part of the code recently.
I also bumped into this trying to construct an update with a self-join and found that I could not create a valid sql update using UpdateManager and resorted to writing the SQL by hand. I am curious why @lazaronixon closed his PR and if there are any thoughts on where this issue goes from here.
Steps to reproduce
complex update with joins produce invalid sql syntax with sqlite and postgres.
the following test it "generates an update statement with joins" is checking an sql query that is invalid in sqlite and postgres.
this is how postgres update syntax can be and how sqlite update syntax
is defined. To join table during an update we should use the
FROM
.it seems that it's a syntax only valid in mysql.
Expected behavior
it should be possible to set the
FROM
to join other tables. and at the end generate the following sqlUPDATE "users" SET "posts_count" = COUNT("posts"."id") FROM "posts" WHERE "users"."id" = "posts"."user_id"
.for example with the following syntax:
Actual behavior
for now, it generate the following sql
UPDATE "users" INNER JOIN "posts" SET "posts_count" = COUNT("posts"."id")
that is an invalid syntaxSystem configuration
Rails version: 7.0.1
Ruby version: 3.1
The text was updated successfully, but these errors were encountered: