# Liquibase + MySQL + Foreign Keys to different database

**URL:** https://forum.liquibase.org/t/liquibase-mysql-foreign-keys-to-different-database/3471
**Category:** General Discussion
**Created:** [January 12, 2017, 2:53am UTC](https://forum.liquibase.org/t/liquibase-mysql-foreign-keys-to-different-database/3471 "2017-01-12T02:53:00Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![un1484159471176r75id](https://avatars.discourse-cdn.com/v4/letter/u/c57346/32.png) [@un1484159471176r75id](https://forum.liquibase.org/u/un1484159471176r75id)
#### Post date: [January 12, 2017, 2:53am UTC](https://forum.liquibase.org/t/liquibase-mysql-foreign-keys-to-different-database/3471/1 "2017-01-12T02:53:00Z")

</div>

Alas, my zeal was underserved :(. &nbsp;Yes - it didn’t crash - and yes it generated a valid XML file. &nbsp;Alas, the schema it generated the XML file was for the database referenced in the --defaultSchemaName option - ignoring the one used in the connector

I’ve created a bug in [liquibase.jira.com](http://liquibase.jira.com) for this issue.

Jim

---

<div class="post-metadata">

### Author: ![un1484159471176r75id](https://avatars.discourse-cdn.com/v4/letter/u/c57346/32.png) [@un1484159471176r75id](https://forum.liquibase.org/u/un1484159471176r75id)
#### Post date: [January 12, 2017, 2:53am UTC](https://forum.liquibase.org/t/liquibase-mysql-foreign-keys-to-different-database/3471/2 "2017-01-12T02:53:00Z")

</div>

I did find the --schemas option which you referenced in your original response. &nbsp;There is such an option. &nbsp;However, its usage is only valid on a diff - not a generate

Jim

---

<div class="post-metadata">

### Author: ![un1382561729492r88id](https://avatars.discourse-cdn.com/v4/letter/u/c4cdca/32.png) [@un1382561729492r88id](https://forum.liquibase.org/u/un1382561729492r88id)
#### Post date: [January 12, 2017, 2:53am UTC](https://forum.liquibase.org/t/liquibase-mysql-foreign-keys-to-different-database/3471/3 "2017-01-12T02:53:00Z")

</div>

Liquibase is really designed to only work with a single schema at a time. If you are working with complex multi-schema databases (or, as MySQL calls them, multiple databases) then you should probably look at Datical DB. The other option is to write your own wrappers around Liquibase to handle the multiple different databases.&nbsp;

Steve Donie  
Principal Software Engineer  
Datical, Inc. [http://www.datical.com/](http://www.datical.com/)

---

<div class="post-metadata">

### Author: ![nvoxland](https://avatars.discourse-cdn.com/v4/letter/n/87869e/32.png) [@nvoxland](https://forum.liquibase.org/u/nvoxland)
#### Post date: [January 12, 2017, 2:53am UTC](https://forum.liquibase.org/t/liquibase-mysql-foreign-keys-to-different-database/3471/4 "2017-01-12T02:53:00Z")

</div>

I do tend to see generateChangeLog as a way to bootstrap using liquibase, but not the primary workflow and so there does tend to be more edge-case issues like handling cross-database foreign keys in mysql. The best way long-term to work with Liquibase is to add to the changelog file yourself during development.

However, the generateChangeLog command shouldn’t completely die in this case. There is a --schemas parameter that can be used to make Liquibase snapshot both schemas/databases which may help since it will see all the objects. Otherwise, open a but a [liquibase.jira.com](http://liquibase.jira.com) and I can take a look at it more.

Nathan

---

<div class="post-metadata">

### Author: ![JimUdall](https://avatars.discourse-cdn.com/v4/letter/j/48db29/32.png) [@JimUdall](https://forum.liquibase.org/u/JimUdall)
#### Post date: [January 12, 2017, 2:53am UTC](https://forum.liquibase.org/t/liquibase-mysql-foreign-keys-to-different-database/3471/5 "2017-01-12T02:53:00Z")

</div>

MySQL has a lovely feature with foreign keys. &nbsp;In particular one can have a foreign key reference a table in a DIFFERENT database. &nbsp;Apparently liquibase PUKES on this. &nbsp;consider the following simple schema:

/\*!40101 SET NAMES utf8 \*/;

/_!40101 SET SQL\_MODE=’’_/;

/\*!40014 SET @OLD\_UNIQUE\_CHECKS=@@UNIQUE\_CHECKS, UNIQUE\_CHECKS=0 \*/;

/\*!40014 SET @OLD\_FOREIGN\_KEY\_CHECKS=@@FOREIGN\_KEY\_CHECKS, FOREIGN\_KEY\_CHECKS=0 \*/;

/\*!40101 SET @OLD\_SQL\_MODE=@@SQL\_MODE, SQL\_MODE=‘NO\_AUTO\_VALUE\_ON\_ZERO’ \*/;

/\*!40111 SET @OLD\_SQL\_NOTES=@@SQL\_NOTES, SQL\_NOTES=0 \*/;

CREATE DATABASE /_!32312 IF NOT EXISTS_/`MyDB` /\*!40100 DEFAULT CHARACTER SET utf8 \*/;

USE `MyDB`;

DROP TABLE IF EXISTS `Users`;

CREATE TABLE `Users` (

&nbsp; `id` INT(11) NOT NULL AUTO\_INCREMENT,

&nbsp; `Accounts_id` INT(11) DEFAULT NULL ,

&nbsp; PRIMARY KEY (`id`),

&nbsp; KEY `fk_Users_Accounts_idx` (`Accounts_id`),

&nbsp; CONSTRAINT `fk_Staffs_Accounts` FOREIGN KEY (`Accounts_id`) REFERENCES `AnotherDB`.`Accounts` (`id`) ON DELETE CASCADE ON UPDATE CASCADE

) ENGINE=INNODB DEFAULT CHARSET=utf8 ;

If you run liquibase generateChangeLog on this schema, then it will die with the following message:

Unexpected error running Liquibase: java.lang.IndexOutOfBoundsException: Index: 0, Size: 0

Obviously my schema is much more complicated than that. &nbsp;But I have reduced it to this simple problem.

Curiously on my REAL schema, I can make subtle changes to certain other aspects of the schema (e.g. rename a column in some unrelated table). &nbsp;And the generateChangeLog will work! &nbsp;Sort of.

Alas if you look at the output changelog file, you will discover all the tables are created with no column definitions added. &nbsp;It does do other things in the resultant XML - like generated indices…But of course if you try to do something like :changeLogSync - it barfs with a bunch of errors - specifically because the columns are all null.

Any comments/suggestions from others on this bug?

Jim

---

<div class="post-metadata">

### Author: ![un1484159471176r75id](https://avatars.discourse-cdn.com/v4/letter/u/c57346/32.png) [@un1484159471176r75id](https://forum.liquibase.org/u/un1484159471176r75id)
#### Post date: [January 12, 2017, 2:53am UTC](https://forum.liquibase.org/t/liquibase-mysql-foreign-keys-to-different-database/3471/6 "2017-01-12T02:53:00Z")

</div>

Well Nathan…YOU DA’ MAN!

I suspect you didn’t exactly mean the --schemas option - I couldn’t find such an option. &nbsp;However, there is the --defaultSchemaName option which DOES solve the problem.

I’m very grateful for you support and help on this issue Nathan

Jim
