MySQL Sequelize.js 外键

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14169655/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 16:03:07  来源:igfitidea点击:

Sequelize.js foreign key

mysqlnode.jsormsequelize.js

提问by swampcypress

When using Sequelize.js, the following code doesn't add any foreign key on tables.

使用 Sequelize.js 时,以下代码不会在表上添加任何外键。

var MainDashboard = sequelize.define('main_dashboard', {
  title: Sequelize.STRING
}, {
  freezeTableName: true
})

MainClient.hasOne(MainDashboard, { foreignKey: 'idClient' })
MainDashboard.hasOne(MainClient, { foreignKey: 'clientId' })

sequelize.sync({ force: true })

Is there any way to force Sequelize.js to add these foreign key constraints?

有没有办法强制 Sequelize.js 添加这些外键约束?

回答by Ilsondotcom

Before I had the same problem, and solved when I understood the functioning of settings Sequelize.

在我遇到同样的问题之前,当我了解设置 Sequelize 的功能后就解决了。

Straight to the point!

开门见山!

Suppose we have two objects: Personand Father

假设我们有两个目标:父亲

var Person = sequelize.define('Person', {

        name: Sequelize.STRING
});

var Father = sequelize.define('Father', {

        age: Sequelize.STRING,
        //The magic start here
        personId: {
              type: Sequelize.INTEGER,
              references: 'persons', // <<< Note, its table's name, not object name
              referencesKey: 'id' // <<< Note, its a column name
        }
});

Person.hasMany(Father); // Set one to many relationship

Maybe it helps you

也许它可以帮助你

Edit:

编辑:

You can read this to understand better:

您可以阅读本文以更好地理解:

http://docs.sequelizejs.com/manual/tutorial/associations.html#foreign-keys

http://docs.sequelizejs.com/manual/tutorial/associations.html#foreign-keys

回答by codejockie

For Sequelize 4 this has been updated to the following:

对于 Sequelize 4,这已更新为以下内容:

const Person = sequelize.define('Person', {
        name: Sequelize.STRING
});

const Father = sequelize.define('Father', {
    age: Sequelize.STRING,
    personId: {
       type: Sequelize.INTEGER,
       references: {
          model: 'persons', // 'persons' refers to table name
          key: 'id', // 'id' refers to column name in persons table
       }
    }
});

Person.hasMany(Father); // Set one to many relationship

Edit: You can read more on associations at https://sequelize.org/master/manual/assocs.html

编辑:您可以在https://sequelize.org/master/manual/assocs.html上阅读有关关联的更多信息

回答by Jan Aagaard Meier

I just tried to run your code, and the rows seem to be created fine:

我只是尝试运行您的代码,行似乎创建得很好:

CREATE TABLE IF NOT EXISTS `main_dashboard` (`title` VARCHAR(255), `id` INTEGER NOT NULL auto_increment , `idClient` INTEGER, PRIMARY KEY (`id`)) ENGINE=InnoDB;
CREATE TABLE IF NOT EXISTS `main_client` (`id` INTEGER NOT NULL auto_increment,  `clientId` INTEGER, PRIMARY KEY (`id`)) ENGINE=InnoDB;

clientIdis added to main_client, and idClientis added to main_dashboard

clientId添加到main_client,并idClient添加到main_dashboard

It seems you have slightly confused what the hasOnemethod does. Each time you call hasOnean association is created, so your code effectively associates the two tables twice. The method you are looking for is belongsTo

似乎您对该hasOne方法的作用有些困惑。每次调用hasOne都会创建一个关联,因此您的代码有效地将两个表关联了两次。您正在寻找的方法是belongsTo

If you want each client to have one dashboard, the code would be the following:

如果您希望每个客户端都有一个仪表板,则代码如下:

MainClient.hasOne(MainDashboard, { foreignKey: 'clientId' })
MainDashboard.belongsTo(MainClient, { foreignKey: 'clientId' })

This creates a clientIdfield on the main_dashboardtable, which relates to the idfield of the main_clienttable

这会clientIdmain_dashboard表上创建一个字段,该id字段与main_client表的字段相关

In short belongsToadds the relation to the table that you are calling the method on, hasOneadds it on the table that is given as argument.

简而言之,belongsTo添加与您正在调用该方法的表的关系,hasOne将其添加到作为参数给出的表中。

回答by Farm

You need to add foreignKeyConstraint: true

你需要添加 foreignKeyConstraint: true

Try:

尝试:

MainClient.hasOne(MainDashboard, { foreignKey: 'idClient', foreignKeyConstraint: true })

回答by Krishan Pal

It's amazingly simple.

这非常简单。

const MainDashboard = this.sequelize.define('main_dashboard', {/* attributes */})
  , MainClient = this.sequelize.define('main_client', {/* attributes */});

MainDashboard.belongsTo(MainClient, { foreignKey: 'clientId' }); // Adds clientId to MainDashboard

It will link this as a foreign key and you may use it as an association. Let me know if I'm missing anything.

它将将此链接为外键,您可以将其用作关联。如果我遗漏了什么,请告诉我。