@@ -2740,11 +2740,6 @@ var CreateForeignKeyTests = []ScriptTest{
27402740 {"fk3" , "mydb" , "child2" , "f" , "mydb" , "child" , "d" , "SET NULL" , "NO ACTION" },
27412741 },
27422742 },
2743- },
2744- },
2745- {
2746- Name : "error cases" ,
2747- Assertions : []ScriptTestAssertion {
27482743 {
27492744 Query : "ALTER TABLE child2 ADD CONSTRAINT fk3 FOREIGN KEY (f) REFERENCES dne(d) ON UPDATE SET NULL" ,
27502745 ExpectedErr : sql .ErrTableNotFound ,
@@ -2789,6 +2784,122 @@ var CreateForeignKeyTests = []ScriptTest{
27892784 },
27902785 },
27912786 },
2787+ {
2788+ Name : "char with foreign key" ,
2789+ Dialect : "mysql" ,
2790+ SetUpScript : []string {
2791+ "create table parent (c char(3) primary key);" ,
2792+ "insert into parent values ('abc'), ('def'), ('ghi');" ,
2793+ },
2794+ Assertions : []ScriptTestAssertion {
2795+ {
2796+ Query : "create table child_char_1 (c char(1), foreign key (c) references parent(c));" ,
2797+ Expected : []sql.Row {
2798+ {types .NewOkResult (0 )},
2799+ },
2800+ },
2801+ {
2802+ Query : "insert into child_char_1 values ('a');" ,
2803+ ExpectedErr : sql .ErrForeignKeyChildViolation ,
2804+ },
2805+ {
2806+ Query : "insert into child_char_1 values ('abc');" ,
2807+ ExpectedErrStr : "string 'abc' is too large for column 'c'" ,
2808+ },
2809+ {
2810+ Skip : true ,
2811+ Query : "create table child_varchar_10 (vc varchar(10), foreign key (vc) references parent(c));" ,
2812+ Expected : []sql.Row {
2813+ {types .NewOkResult (0 )},
2814+ },
2815+ },
2816+ {
2817+ Skip : true ,
2818+ Query : "insert into child_varchar_10 values ('abc');" ,
2819+ Expected : []sql.Row {
2820+ {types .NewOkResult (1 )},
2821+ },
2822+ },
2823+ {
2824+ Skip : true ,
2825+ Query : "insert into child_varchar_10 values ('abcdefghij');" ,
2826+ ExpectedErr : sql .ErrForeignKeyChildViolation ,
2827+ },
2828+ },
2829+ },
2830+ {
2831+ Name : "binary with foreign key" ,
2832+ Dialect : "mysql" ,
2833+ SetUpScript : []string {
2834+ "create table parent (b binary(3) primary key);" ,
2835+ "insert into parent values ('abc'), ('def'), ('ghi');" ,
2836+ },
2837+ Assertions : []ScriptTestAssertion {
2838+ {
2839+ Query : "create table child_binary_1 (b binary(1), foreign key (b) references parent(b));" ,
2840+ Expected : []sql.Row {
2841+ {types .NewOkResult (0 )},
2842+ },
2843+ },
2844+ {
2845+ Query : "insert into child_binary_1 values ('a');" ,
2846+ ExpectedErr : sql .ErrForeignKeyChildViolation ,
2847+ },
2848+ {
2849+ Query : "insert into child_binary_1 values ('abc');" ,
2850+ ExpectedErrStr : "string 'abc' is too large for column 'b'" ,
2851+ },
2852+ {
2853+ Skip : true ,
2854+ Query : "create table child_varbinary_10 (vb varbinary(10), foreign key (vb) references parent(b));" ,
2855+ Expected : []sql.Row {
2856+ {types .NewOkResult (0 )},
2857+ },
2858+ },
2859+ {
2860+ Skip : true ,
2861+ Query : "insert into child_varbinary_10 values ('abc');" ,
2862+ Expected : []sql.Row {
2863+ {types .NewOkResult (1 )},
2864+ },
2865+ },
2866+ {
2867+ Skip : true ,
2868+ Query : "insert into child_varbinary_10 values ('abcdefghij');" ,
2869+ ExpectedErr : sql .ErrForeignKeyChildViolation ,
2870+ },
2871+ },
2872+ },
2873+ {
2874+ Name : "mixed int type foreign key tests" ,
2875+ Dialect : "mysql" ,
2876+ SetUpScript : []string {
2877+ "create table parent (i int primary key);" ,
2878+ "insert into parent values (1), (2), (3);" ,
2879+ },
2880+ Assertions : []ScriptTestAssertion {
2881+ {
2882+ Query : "create table child_tinyint (ti tinyint, foreign key (ti) references parent (i));" ,
2883+ ExpectedErr : sql .ErrForeignKeyColumnTypeMismatch ,
2884+ },
2885+ {
2886+ Query : "create table child_smallint (si tinyint, foreign key (si) references parent (i));" ,
2887+ ExpectedErr : sql .ErrForeignKeyColumnTypeMismatch ,
2888+ },
2889+ {
2890+ Query : "create table child_mediumint (mi tinyint, foreign key (mi) references parent (i));" ,
2891+ ExpectedErr : sql .ErrForeignKeyColumnTypeMismatch ,
2892+ },
2893+ {
2894+ Query : "create table child_bigint (ti tinyint, foreign key (ti) references parent (i));" ,
2895+ ExpectedErr : sql .ErrForeignKeyColumnTypeMismatch ,
2896+ },
2897+ {
2898+ Query : "create table child_unsigned (i int unsigned, foreign key (i) references parent (i));" ,
2899+ ExpectedErr : sql .ErrForeignKeyColumnTypeMismatch ,
2900+ },
2901+ },
2902+ },
27922903}
27932904
27942905var DropForeignKeyTests = []ScriptTest {
@@ -2831,11 +2942,6 @@ var DropForeignKeyTests = []ScriptTest{
28312942 RC.TABLE_NAME = KCU.TABLE_NAME AND RC.REFERENCED_TABLE_NAME = KCU.REFERENCED_TABLE_NAME;` ,
28322943 Expected : []sql.Row {},
28332944 },
2834- },
2835- },
2836- {
2837- Name : "error cases" ,
2838- Assertions : []ScriptTestAssertion {
28392945 {
28402946 Query : "ALTER TABLE child3 DROP CONSTRAINT dne" ,
28412947 ExpectedErr : sql .ErrTableNotFound ,
0 commit comments