Skip to content

write_lobs fix: handle table with composite primary key #2103

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,14 @@ def empty_insert_statement_value

# Writes LOB values from attributes for specified columns
def write_lobs(table_name, klass, attributes, columns) #:nodoc:
id = quote(attributes[klass.primary_key])
if klass.primary_key.kind_of?(Array)
id_conditions = klass.primary_key.map { |key|
"#{quote_table_name(table_name)}.#{quote_column_name(key)} = #{quote(attributes[key])}"
}.join(' AND ')
else
id_conditions = "#{quote_table_name(table_name)}.#{quote_column_name(klass.primary_key)} = #{quote(attributes[klass.primary_key])}"
end

columns.each do |col|
value = attributes[col.name]
# changed sequence of next two lines - should check if value is nil before converting to yaml
Expand All @@ -263,7 +270,7 @@ def write_lobs(table_name, klass, attributes, columns) #:nodoc:
uncached do
unless lob_record = select_one(sql = <<~SQL.squish, "Writable Large Object")
SELECT #{quote_column_name(col.name)} FROM #{quote_table_name(table_name)}
WHERE #{quote_column_name(klass.primary_key)} = #{id} FOR UPDATE
WHERE #{id_conditions} FOR UPDATE
SQL
raise ActiveRecord::RecordNotFound, "statement #{sql} returned no rows"
end
Expand Down