[MDEV-9142] Adding Constraint with no database reference results in ERROR 1046 (3D000) at line 13: No database selected Created: 2015-11-17  Updated: 2016-04-23  Resolved: 2016-04-23

Status: Closed
Project: MariaDB Server
Component/s: Data Definition - Alter Table
Affects Version/s: 5.5.41, 5.5.46, 5.5, 10.0, 10.1
Fix Version/s: 5.5.50, 10.0.25

Type: Bug Priority: Minor
Reporter: Martin Walsh Assignee: Jan Lindström (Inactive)
Resolution: Fixed Votes: 0
Labels: upstream
Environment:

Tested on Windows 2012, CentOS6.7



 Description   

When creating a table with a constraint which references a table without explicitly naming the database the statement fails with the following error.

ERROR 1046 (3D000) at line ##@ No database selected.

The test statement used is as follows:

CREATE TABLE `kg_test1`.`person` 
(
 `Id` INT(11) NOT NULL AUTO_INCREMENT,
 `Name` VARCHAR(50) NOT NULL,
  PRIMARY KEY (`Id`),
  CONSTRAINT `fk_person_group` FOREIGN KEY (`Id`) REFERENCES `group` (`Id`)
) ENGINE=INNODB DEFAULT CHARSET=utf8;

I'm using a local client to connect and run the test statement on MariaDB 5.5.41 - 46, on both Windows 2012 & CentOS 6.7....which all fail with the same error as above.

You can work around this by either adding a database name to the reference, (kg_test1.group) or adding database context (i.e. use <db-name>) when logging in via the client, but it does not have to be the same database the table is being created in.

This is a change in behaviour to MariaDB verisons prior to 5.5.41.



 Comments   
Comment by Elena Stepanova [ 2015-11-19 ]

The change was introduced by this commit:

commit 0b28d7e048fa097280be54f9baffd202f7626bdd
Author: Praveenkumar Hulakund <praveenkumar.hulakund@oracle.com>
Date:   Wed Sep 10 10:50:17 2014 +0530
 
    Bug#18790730 - CROSS-DATABASE FOREIGN KEY WITHOUT PERMISSIONS
                   CHECK.
    
    Analysis:
    ----------
    Issue here is, while creating or altering the InnoDB table,
    if the foreign key defined on the table references a parent
    table on which the user has no access privileges then the
    table is created without reporting any error.
    
    Currently the privilege level REFERENCES_ACL is unused
    and is not used for access evaluation while creating the
    table with a foreign key constraint or adding the foreign
    key constraint to a table. But when no privileges are granted
    to user then also access evaluation on parent table is ignored.
    
    Fix:
    ---------
    For DMLs, irrelevant of the fact, support does not want any
    changes to avoid permission checks on every operation.
    
    So, as a fix, added a function "check_fk_parent_table_access"
    to check whether any of the SELECT_ACL, INSERT_ACL, UDPATE_ACL,
    DELETE_ACL or REFERENCE_ACL privileges are granted for user
    at table level. If none of them is granted then error is reported.
    This function is called during the table creation and alter
    operation.

However, I can't say from the above whether the described side-effect is expected or not. Assigning to jplindst for an expert opinion.

Here is the complete test case:

--source include/have_innodb.inc
DROP DATABASE test;
CREATE DATABASE kg_test1;
CREATE TABLE `kg_test1`.`group` (
  Id INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
 
CREATE TABLE `kg_test1`.`person` (
`Id` INT(11) NOT NULL AUTO_INCREMENT,
`Name` VARCHAR(50) NOT NULL,
PRIMARY KEY (`Id`),
CONSTRAINT `fk_person_group` FOREIGN KEY (`Id`) REFERENCES `group` (`Id`)
) ENGINE=INNODB DEFAULT CHARSET=utf8;
SHOW WARNINGS;
DROP DATABASE kg_test1;
CREATE DATABASE test;

Comment by Jan Lindström (Inactive) [ 2016-04-07 ]

commit 8bd6088800240a4e74ee58116f58d559cc4fc9f2
Author: Jan Lindström <jan.lindstrom@mariadb.com>
Date: Thu Apr 7 10:47:46 2016 +0300

MDEV-9142 :Adding Constraint with no database reference
results in ERROR 1046 (3D000) at line 13: No database selected.

Use database from create table to foreign key database if
nothing else is given.

Comment by Jan Lindström (Inactive) [ 2016-04-23 ]

commit 19e3597e0c718a4cfdfe8789c7b4b11a4e0ba0c6
Author: Jan Lindström <jan.lindstrom@mariadb.com>
Date: Thu Apr 7 10:47:46 2016 +0300

MDEV-9142 :Adding Constraint with no database reference
results in ERROR 1046 (3D000) at line 13: No database selected.

Use database from create table to foreign key database if
nothing else is given.

Generated at Thu Feb 08 07:32:27 UTC 2024 using Jira 8.20.16#820016-sha1:9d11dbea5f4be3d4cc21f03a88dd11d8c8687422.