简体   繁体   English

如何使用sequelize和mysql创建一对一关系?

[英]How to create a one-to-one relationship using sequelize and mysql?

I am trying to create a one-to-one relationship between two entities. 我试图在两个实体之间建立一对一的关系。 I am using sequelize as ORM to the database mysql . 我正在将sequelize用作数据库mysql ORM。

My model definitions are the following: 我的模型定义如下:

var user = (sequelize, DataTypes) => {                                                                               
  const User = sequelize.define('user', {                                                                            
    name: {type: DataTypes.STRING, allowNull: false, unique: true }                                                  
  }, {                                                                                                               
    timestamps: false                                                                                                
  });                                                                                                                

  return User;                                                                                                       
};                                                                                                                   

var product = (sequelize, DataTypes) => {                                                                            
  const Product = sequelize.define('product', {                                                                      
    title: {type: DataTypes.STRING, allowNull: false, unique: true, default: 'Coop Default' }                        
  }, {                                                                                                               
    timestamps: false                                                                                                
  });                                                                                                                

  return Product;                                                                                                    
};                                                                                                                   

and one-to-one relation is made with the following lines of code 通过以下几行代码建立一对一的关系

db.product.belongsTo(db.user);                                                                                       
db.user.hasOne(db.product);  

But, this does not create a unique foreign key in the table products . 但是,这不会在表products创建唯一的外键。

mysql> desc products;
+--------+--------------+------+-----+---------+----------------+
| Field  | Type         | Null | Key | Default | Extra          |
+--------+--------------+------+-----+---------+----------------+
| id     | int(11)      | NO   | PRI | NULL    | auto_increment |
| title  | varchar(255) | NO   | UNI | NULL    |                |
| userId | int(11)      | YES  | MUL | NULL    |                |
+--------+--------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)

What else should I do in the ORM to ensure userId is unique? 我应该在ORM中做什么以确保userId是唯一的?

I am able to force the unique constraint on the foreignKey by mentioning it in the model definition, apparently, sequelize is not able to do that from the associations. 通过在模型定义中提到它,我可以对ForeignKey施加unique约束,显然,sequelize无法从关联中做到这一点。

var product = (sequelize, DataTypes) => {                                                                            
  const Product = sequelize.define('product', {                                                                      
    title: { type: DataTypes.STRING,                                                                                 
             allowNull: false,                                                                                       
             unique: true                                                                                            
           },                                                                                                        

    userId: { type: DataTypes.INTEGER,                                                                                  
           allowNull: false,                                                                                         
           unique: true                                                                                              
         }                                                                                                           
  }, {                                                                                                               
    timestamps: false                                                                                                
  });                                                                                                                

  return Product;                                                                                                    
};                                                                                                                   

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM