Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
rails
提交
59c3ed1b
R
rails
项目概览
张重言
/
rails
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
rails
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
59c3ed1b
编写于
7月 30, 2018
作者:
G
George Claghorn
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
MySQL: Raise ActiveRecord::InvalidForeignKey for foreign-key constraint violations on delete
上级
cfee9fee
变更
3
显示空白变更内容
内联
并排
Showing
3 changed file
with
20 addition
and
7 deletion
+20
-7
activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb
...tive_record/connection_adapters/abstract_mysql_adapter.rb
+2
-1
activerecord/lib/active_record/errors.rb
activerecord/lib/active_record/errors.rb
+2
-1
activerecord/test/cases/adapter_test.rb
activerecord/test/cases/adapter_test.rb
+16
-5
未找到文件。
activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb
浏览文件 @
59c3ed1b
...
...
@@ -619,6 +619,7 @@ def extract_precision(sql_type)
ER_DUP_ENTRY
=
1062
ER_NOT_NULL_VIOLATION
=
1048
ER_DO_NOT_HAVE_DEFAULT
=
1364
ER_ROW_IS_REFERENCED_2
=
1451
ER_NO_REFERENCED_ROW_2
=
1452
ER_DATA_TOO_LONG
=
1406
ER_OUT_OF_RANGE
=
1264
...
...
@@ -633,7 +634,7 @@ def translate_exception(exception, message)
case
error_number
(
exception
)
when
ER_DUP_ENTRY
RecordNotUnique
.
new
(
message
)
when
ER_NO_REFERENCED_ROW_2
when
ER_
ROW_IS_REFERENCED_2
,
ER_
NO_REFERENCED_ROW_2
InvalidForeignKey
.
new
(
message
)
when
ER_CANNOT_ADD_FOREIGN
mismatched_foreign_key
(
message
)
...
...
activerecord/lib/active_record/errors.rb
浏览文件 @
59c3ed1b
...
...
@@ -111,7 +111,8 @@ class WrappedDatabaseException < StatementInvalid
class
RecordNotUnique
<
WrappedDatabaseException
end
# Raised when a record cannot be inserted or updated because it references a non-existent record.
# Raised when a record cannot be inserted or updated because it references a non-existent record,
# or when a record cannot be deleted because a parent record references it.
class
InvalidForeignKey
<
WrappedDatabaseException
end
...
...
activerecord/test/cases/adapter_test.rb
浏览文件 @
59c3ed1b
...
...
@@ -304,6 +304,8 @@ def test_supports_multi_insert_is_deprecated
class
AdapterForeignKeyTest
<
ActiveRecord
::
TestCase
self
.
use_transactional_tests
=
false
fixtures
:fk_test_has_pk
def
setup
@connection
=
ActiveRecord
::
Base
.
connection
end
...
...
@@ -322,7 +324,7 @@ def test_foreign_key_violations_are_translated_to_specific_exception_with_valida
assert_not_nil
error
.
cause
end
def
test_foreign_key_violations_are_translated_to_specific_exception
def
test_foreign_key_violations_
on_insert_
are_translated_to_specific_exception
error
=
assert_raises
(
ActiveRecord
::
InvalidForeignKey
)
do
insert_into_fk_test_has_fk
end
...
...
@@ -330,6 +332,16 @@ def test_foreign_key_violations_are_translated_to_specific_exception
assert_not_nil
error
.
cause
end
def
test_foreign_key_violations_on_delete_are_translated_to_specific_exception
insert_into_fk_test_has_fk
fk_id:
1
error
=
assert_raises
(
ActiveRecord
::
InvalidForeignKey
)
do
@connection
.
execute
"DELETE FROM fk_test_has_pk WHERE pk_id = 1"
end
assert_not_nil
error
.
cause
end
def
test_disable_referential_integrity
assert_nothing_raised
do
@connection
.
disable_referential_integrity
do
...
...
@@ -342,14 +354,13 @@ def test_disable_referential_integrity
end
private
def
insert_into_fk_test_has_fk
def
insert_into_fk_test_has_fk
(
fk_id:
0
)
# Oracle adapter uses prefetched primary key values from sequence and passes them to connection adapter insert method
if
@connection
.
prefetch_primary_key?
id_value
=
@connection
.
next_sequence_value
(
@connection
.
default_sequence_name
(
"fk_test_has_fk"
,
"id"
))
@connection
.
execute
"INSERT INTO fk_test_has_fk (id,fk_id) VALUES (
#{
id_value
}
,
0
)"
@connection
.
execute
"INSERT INTO fk_test_has_fk (id,fk_id) VALUES (
#{
id_value
}
,
#{
fk_id
}
)"
else
@connection
.
execute
"INSERT INTO fk_test_has_fk (fk_id) VALUES (
0
)"
@connection
.
execute
"INSERT INTO fk_test_has_fk (fk_id) VALUES (
#{
fk_id
}
)"
end
end
end
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录