Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When I use Model.hasOne function to create a Association, I found a weird thing #70

Open
lionel9826 opened this issue Jul 30, 2021 · 0 comments

Comments

@lionel9826
Copy link

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
image
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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant