Skip to content

Commit c4bbbe2

Browse files
committed
Fix some sqlite3 foreign key tests
1 parent 54c5ff9 commit c4bbbe2

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

lib/arjdbc/sqlite3/adapter.rb

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -354,14 +354,23 @@ def add_reference(table_name, ref_name, **options) # :nodoc:
354354
alias :add_belongs_to :add_reference
355355

356356
def foreign_keys(table_name)
357+
# SQLite returns 1 row for each column of composite foreign keys.
357358
fk_info = internal_exec_query("PRAGMA foreign_key_list(#{quote(table_name)})", "SCHEMA")
358-
fk_info.map do |row|
359+
grouped_fk = fk_info.group_by { |row| row["id"] }.values.each { |group| group.sort_by! { |row| row["seq"] } }
360+
grouped_fk.map do |group|
361+
row = group.first
359362
options = {
360-
column: row["from"],
361-
primary_key: row["to"],
362363
on_delete: extract_foreign_key_action(row["on_delete"]),
363364
on_update: extract_foreign_key_action(row["on_update"])
364365
}
366+
367+
if group.one?
368+
options[:column] = row["from"]
369+
options[:primary_key] = row["to"]
370+
else
371+
options[:column] = group.map { |row| row["from"] }
372+
options[:primary_key] = group.map { |row| row["to"] }
373+
end
365374
# DIFFERENCE: FQN
366375
::ActiveRecord::ConnectionAdapters::ForeignKeyDefinition.new(table_name, row["table"], options)
367376
end

0 commit comments

Comments
 (0)