sequelize / sequelize/sequelize
Foreign key reference a column in the same table causes error
Open
Nobody has claimed this yet.
dialect: mysql
dialect: postgres
type: bug
- Dominant language
- TypeScript
- Stars
- 30.4k
- Forks
- 4.3k
- Avg merge
- 1d 6h
- Merged PRs (30d)
- 68
Description
What are you doing?
module.exports = (sequelize, DataTypes) => {
class Test extends sequelize.Sequelize.Model { }
Test.init({
id: {
field: 'id',
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: DataTypes.BIGINT(11)
},
userId1: {
field: 'user_id_1',
allowNull: false,
type: DataTypes.STRING(36),
},
userId2: {
field: 'user_id_2',
allowNull: false,
type: DataTypes.STRING(64),
},
}, { indexes: [
{ unique: true, fields: [ 'user_id_1' ]},
{ fields: ['user_id_2' ]}
],
underscored: true,
sequelize, modelName: 'Test' });
Test.associate = ({ Test }) => {
Test.hasMany(Test, { sourceKey:'userId2', targetKey: 'userId1', foreignKey: 'user_id_2' });
};
return Test;
};
To Reproduce
Steps to reproduce the behavior:
Run Test.sync()
What do you expect to happen?
CREATE TABLE `tests` (
`id` bigint(11) NOT NULL AUTO_INCREMENT,
`user_id_1` varchar(36) NOT NULL,
`user_id_2` varchar(64) NOT NULL,
`created_at` datetime NOT NULL,
`updated_at` datetime NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `tests_user_id_1` (`user_id_1`),
KEY `tests_user_id_2` (`user_id_2`),
CONSTRAINT `fk_userid1_userid2` FOREIGN KEY (`user_id_2`) REFERENCES `tests` (`user_id_1`)
ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
What is actually happening?
CREATE TABLE IF NOT EXISTS `tests` (
`id` BIGINT(11) NOT NULL auto_increment ,
`user_id_1` VARCHAR(36) NOT NULL,
`user_id_2` VARCHAR(36),
`created_at` DATETIME NOT NULL,
`updated_at` DATETIME NOT NULL,
PRIMARY KEY (`id`),
FOREIGN KEY (`user_id_2`) REFERENCES `tests` (`user_id_1`)
ON DELETE SET NULL ON UPDATE CASCADE
) ENGINE=InnoDB;
Unhandled rejection SequelizeDatabaseError: Can't create table `test_bug`.`tests` (errno: 150 "Foreign key constraint is incorrectly formed")
Environment
Dialect:
- mysql
Dialect library version: mysql2: "^1.6.5"
Database version: '10.1.13-MariaDB'
Sequelize version: 5.10.1
Node Version: 8.9.1
OS: windows
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by running the provided Test.sync() reproduction against the stated MariaDB environment and compare the generated CREATE TABLE SQL with the expected SQL. Trace the foreign-key generation and same-table association handling until the column type and constraint options are preserved; done means the table is created successfully with the expected reference and actions.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- javascript, mariadb, mysql, nodejs
- Domain
- backend, database
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100