parser
parser copied to clipboard
FK with no default database gives unexpected error
Bug Report
Please answer these questions before submitting your issue. Thanks!
- What did you do? If possible, provide a recipe for reproducing the error.
drop database if exists fk_test;
create database if not exists fk_test;
create table test.t1 (c1 int not null primary key);
create table test.t2 (id int not null primary key, c1 int not null, constraint foreign key (c1) references t1(c1))
- What did you expect to see?
In MySQL, the create table statement with the foreign key constraint does not cause an error. Presumably, MySQL assumes that the parent table is in the same database as the child table.
- What did you see instead?
TiDB Server issues an error in this case.
$ cat fk_test.sql
drop database if exists fk_test;
create database if not exists fk_test;
create table fk_test.t1 (c1 int not null primary key);
create table fk_test.t2 (id int not null primary key, c1 int not null, constraint foreign key (c1) references t1(c1))
$ mysql --verbose -P 3306 --database= < fk_test.sql
--------------
drop database if exists fk_test
--------------
--------------
create database if not exists fk_test
--------------
--------------
create table fk_test.t1 (c1 int not null primary key)
--------------
--------------
create table fk_test.t2 (id int not null primary key, c1 int not null, constraint foreign key (c1) references t1(c1))
--------------
$ mysql --verbose -P 4000 --database= < fk_test.sql
--------------
drop database if exists fk_test
--------------
--------------
create database if not exists fk_test
--------------
--------------
create table fk_test.t1 (c1 int not null primary key)
--------------
--------------
create table fk_test.t2 (id int not null primary key, c1 int not null, constraint foreign key (c1) references t1(c1))
--------------
ERROR 1046 (3D000) at line 4: No database selected
I encountered this issue when trying to load the "employees" sample database (https://github.com/datacharmer/test_db.git) into MySQL->DM->TiDB.
- What version of TiDB SQL Parser are you using?
Release Version: v3.0.0-beta.1-77-g1e5f62096
Git Commit Hash: 1e5f620966370f3bc5c48d78c17bcd3a3234066f
Git Branch: master
UTC Build Time: 2019-04-08 11:09:58
GoVersion: go version go1.12.1 darwin/amd64
Race Enabled: false
TiKV Min Version: 2.1.0-alpha.1-ff3dd160846b7d1aed9079c389fc188f7f5ea13e
Check Table Before Drop: false
github.com/pingcap/parser v0.0.0-20190408064140-cdceeb2c5476
thanks @kolbe, @bb7133 will solve this issue.