File tree Expand file tree Collapse file tree 5 files changed +32
-14
lines changed
lib/active_record/connection_adapters Expand file tree Collapse file tree 5 files changed +32
-14
lines changed Original file line number Diff line number Diff line change @@ -491,15 +491,15 @@ def foreign_keys(table_name)
491
491
492
492
fk_info . map do |row |
493
493
options = {
494
- column : row [ "column" ] ,
494
+ column : unquote_identifier ( row [ "column" ] ) ,
495
495
name : row [ "name" ] ,
496
496
primary_key : row [ "primary_key" ]
497
497
}
498
498
499
499
options [ :on_update ] = extract_foreign_key_action ( row [ "on_update" ] )
500
500
options [ :on_delete ] = extract_foreign_key_action ( row [ "on_delete" ] )
501
501
502
- ForeignKeyDefinition . new ( table_name , row [ "to_table" ] , options )
502
+ ForeignKeyDefinition . new ( table_name , unquote_identifier ( row [ "to_table" ] ) , options )
503
503
end
504
504
end
505
505
Original file line number Diff line number Diff line change @@ -54,6 +54,14 @@ def quoted_binary(value)
54
54
"x'#{ value . hex } '"
55
55
end
56
56
57
+ def unquote_identifier ( identifier )
58
+ if identifier && identifier . start_with? ( "`" )
59
+ identifier [ 1 ..-2 ]
60
+ else
61
+ identifier
62
+ end
63
+ end
64
+
57
65
# Override +type_cast+ we pass to mysql2 Date and Time objects instead
58
66
# of Strings since mysql2 is able to handle those classes more efficiently.
59
67
def type_cast ( value ) # :nodoc:
Original file line number Diff line number Diff line change @@ -531,7 +531,7 @@ def foreign_keys(table_name)
531
531
532
532
fk_info . map do |row |
533
533
options = {
534
- column : row [ "column" ] ,
534
+ column : Utils . unquote_identifier ( row [ "column" ] ) ,
535
535
name : row [ "name" ] ,
536
536
primary_key : row [ "primary_key" ]
537
537
}
@@ -541,8 +541,9 @@ def foreign_keys(table_name)
541
541
options [ :deferrable ] = extract_foreign_key_deferrable ( row [ "deferrable" ] , row [ "deferred" ] )
542
542
543
543
options [ :validate ] = row [ "valid" ]
544
+ to_table = Utils . unquote_identifier ( row [ "to_table" ] )
544
545
545
- ForeignKeyDefinition . new ( table_name , row [ " to_table" ] , options )
546
+ ForeignKeyDefinition . new ( table_name , to_table , options )
546
547
end
547
548
end
548
549
Original file line number Diff line number Diff line change @@ -12,7 +12,7 @@ class Name # :nodoc:
12
12
attr_reader :schema , :identifier
13
13
14
14
def initialize ( schema , identifier )
15
- @schema , @identifier = unquote ( schema ) , unquote ( identifier )
15
+ @schema , @identifier = Utils . unquote_identifier ( schema ) , Utils . unquote_identifier ( identifier )
16
16
end
17
17
18
18
def to_s
@@ -40,15 +40,6 @@ def hash
40
40
def parts
41
41
@parts ||= [ @schema , @identifier ] . compact
42
42
end
43
-
44
- private
45
- def unquote ( part )
46
- if part && part . start_with? ( '"' )
47
- part [ 1 ..-2 ]
48
- else
49
- part
50
- end
51
- end
52
43
end
53
44
54
45
module Utils # :nodoc:
@@ -74,6 +65,14 @@ def extract_schema_qualified_name(string)
74
65
end
75
66
PostgreSQL ::Name . new ( schema , table )
76
67
end
68
+
69
+ def unquote_identifier ( identifier )
70
+ if identifier && identifier . start_with? ( '"' )
71
+ identifier [ 1 ..-2 ]
72
+ else
73
+ identifier
74
+ end
75
+ end
77
76
end
78
77
end
79
78
end
Original file line number Diff line number Diff line change @@ -340,6 +340,16 @@ def test_foreign_key_exists
340
340
assert_not @connection . foreign_key_exists? ( :astronauts , :stars )
341
341
end
342
342
343
+ def test_foreign_key_exists_referencing_table_having_keyword_as_name
344
+ @connection . create_table :user , force : true
345
+ @connection . add_column :rockets , :user_id , :bigint
346
+ @connection . add_foreign_key :rockets , :user
347
+ assert @connection . foreign_key_exists? ( :rockets , :user )
348
+ ensure
349
+ @connection . remove_foreign_key :rockets , :user
350
+ @connection . drop_table :user
351
+ end
352
+
343
353
def test_foreign_key_exists_by_column
344
354
@connection . add_foreign_key :astronauts , :rockets , column : "rocket_id"
345
355
You can’t perform that action at this time.
0 commit comments