sequelize/sequelize — pull request #1817 by jbuck (2014-05-26 19:20:33+00:00)
Title: Sequelize 1.7.5 w/ mysql creates inline foreign keys when foreign key is also a primary key

Example code:

```javascript
var Sequelize = require("sequelize");

var db = new Sequelize("login", "root", "root");

var Parent = db.define('Parent', {}, { timestamps: false });

var Child = db.define('Child', {
parent_id: {
type: Sequelize.INTEGER,
allowNull: false,
primaryKey: true,
references: "Parents",
referencesKey: "id"
}
}, { timestamps: false });

db.sync({force: true, logging: console.log});

```

If I run this example code, get the following DDL:

```sql
DROP TABLE IF EXISTS `Parents`;
CREATE TABLE IF NOT EXISTS `Parents` (`id` INTEGER NOT NULL auto_increment , PRIMARY KEY (`id`)) ENGINE=InnoDB;
DROP TABLE IF EXISTS `Childs`;
CREATE TABLE IF NOT EXISTS `Childs` (`parent_id` INTEGER NOT NULL REFERENCES `Parents` (`id`), PRIMARY KEY (`parent_id`)) ENGINE=InnoDB;
```

Why is that foreign key being referenced inline? MySQL doesn't support that!

The answer is in [/lib/dialects/mysql/query-generator.js#L55-L65](https://github.com/sequelize/sequelize/blob/ea46b9c7667a44770cf227df62f276f376205af0/lib/dialects/mysql/query-generator.js#L55-L65):

```javascript
if (Utils._.includes(dataType, 'PRIMARY KEY')) {
primaryKeys.push(attr)
attrStr.push(this.quoteIdentifier(attr) + " " + dataType.replace(/PRIMARY KEY/, ''))
} else if (Utils._.includes(dataType, 'REFERENCES')) {
// MySQL doesn't support inline REFERENCES declarations: move to the end
var m = dataType.match(/^(.+) (REFERENCES.*)$/)
attrStr.push(this.quoteIdentifier(attr) + " " + m[1])
foreignKeys[attr] = m[2]
} else {
attrStr.push(this.quoteIdentifier(attr) + " " + dataType)
}
```

If the foreign key is also a primary key then it'll never reach that middle block, and add the foreign key to the end of the table definition.

I have a fix for this, I'll submit a PR shortly.