sequelize-docs-Zh-CN
sequelize-docs-Zh-CN copied to clipboard
When I use Model.hasOne function to create a Association, I found a weird thing
My sequelize version is '6.6.5'. When I use hasOne function to create a foreignKey in mariadb, I try to use foreignKey to customize my foreign key name. but it still will create a column named by sourceModel+primary key. here is my code:
User.hasOne(Student, { foreignKey: 'stuid', sourceKey: 'id', })
Student.belongsTo(User)
and here is the result
the sql query string is :
CREATE TABLE IF NOT EXISTS `students` (`id` INTEGER NOT NULL auto_increment , `name` VARCHAR(12) NOT NULL, `grade` VARCHAR(4), `school` VARCHAR(100), `tel` VARCHAR(11), `qq` VARCHAR(20), `email` VARCHAR(50), `addr` VARCHAR(100), `createdAt` DATETIME NOT NULL, `stuid` CHAR(36) BINARY, `UserId` CHAR(36) BINARY, PRIMARY KEY (`id`), FOREIGN KEY (`stuid`) REFERENCES `Users` (`id`) ON DELETE SET NULL ON UPDATE CASCADE, FOREIGN KEY (`UserId`) REFERENCES `Users` (`id`) ON DELETE SET NULL ON UPDATE CASCADE) ENGINE=InnoDB DEFAULT CHARSET=utf8;
And I tried to pass a Object with name:stuid
, but it's still don't work.
By the way, did I have some way to create a foreignKey and make this foreignKey be the target model's primaryKey. if I don't create a primary key for the model, it would default create a primary key named by id. But I want to use the column of another table to be this table's primaryKey