sequelize使用targetKey

时间:2016-05-03 12:07:02

标签: sequelize.js

根据文档,我可以使用targetKeys选项创建另一个模型的foreignKey到特定列。我试图在我的以下模型中这样做:

module.exports = function(sequelize, DataTypes) {
  var Booking = sequelize.define("Booking", {
    //id:           {type:DataTypes.INTEGER, primaryKey:true},        // auto-generate
    bookingId:      {type:DataTypes.INTEGER, primaryKey:true},
    accId:          {type:DataTypes.INTEGER},
    description:    {type:DataTypes.STRING(128)},
    amount:         {type:DataTypes.DECIMAL(15,2), allowNull:false},  // 0:prepaid, 1:postpaid
    schDate:        {type:DataTypes.DATE, allowNull:false},
    createdBy:      {type:DataTypes.INTEGER, allowNull:false},
    modifiedBy:     {type:DataTypes.INTEGER, allowNull:false}
  },
  {
    tableName: "user_bookings",
    timestamps: true,
    paranoid: true,
    classMethods: {
      associate: function(models) {
        Booking.belongsTo(models.Account, {foreignKey:'accId', targetKey:'accId'});
      }
    }
  }
);
  return Booking;
};

我的其他表格如下:

   module.exports = function(sequelize, DataTypes) {
  var Account = sequelize.define("Account", {
    //id:           {type:DataTypes.INTEGER, primaryKey:true},                  // auth-generate
   userId:         {type:DataTypes.INTEGER, primaryKey:true},
    accType:        {type:DataTypes.CHAR(1), allowNull:false},                  // 0:prepaid, 1:postpaid
    category:       {type:DataTypes.INTEGER},                                   // reserved for future use
    isDefault:      {type:DataTypes.BOOLEAN, defaultValue:false},               // Is this the default wallet, (if the user has many wallets. )
    balance:        {type:DataTypes.DECIMAL(15,2), defaultValue:null},
    creditLimit:    {type:DataTypes.DECIMAL(15,2), defaultValue:null},
    creditConsumed: {type:DataTypes.DECIMAL(15,2), defaultValue:null},
    createdBy:      {type:DataTypes.INTEGER, allowNull:false},
    modifiedBy:     {type:DataTypes.INTEGER, allowNull:false},
    createdAt:      {type:DataTypes.DATE, allowNull:false, get:function(){return this.getDataValue('createdAt').getTime()/1000;}},
    updatedAt:      {type:DataTypes.DATE, allowNull:false, get:function(){return this.getDataValue('updatedAt').getTime()/1000;}}
  },
  {
    tableName: "user_accounts",
    timestamps: true,
    paranoid: true,
  }
);
  return Account;
};

创建这些表时,我希望第一个模型将为第二个模型创建一个foreignKey。但事情并没有发生。

我的user_bookings表显示为没有任何引用的独立表:

mysql> desc user_bookings;
+-------------+---------------+------+-----+---------+-------+
| Field       | Type          | Null | Key | Default | Extra |
+-------------+---------------+------+-----+---------+-------+
| bookingId   | int(11)       | NO   | PRI | 0       |       |
| accId       | int(11)       | YES  |     | NULL    |       |
| description | varchar(128)  | YES  |     | NULL    |       |
| amount      | decimal(15,2) | NO   |     | NULL    |       |
| schDate     | datetime      | NO   |     | NULL    |       |
| createdBy   | int(11)       | NO   |     | NULL    |       |
| modifiedBy  | int(11)       | NO   |     | NULL    |       |
| createdAt   | datetime      | NO   |     | NULL    |       |
| updatedAt   | datetime      | NO   |     | NULL    |       |
| deletedAt   | datetime      | YES  |     | NULL    |       |
+-------------+---------------+------+-----+---------+-------+
10 rows in set (0.01 sec) 

我在这里做错了什么?提前谢谢......

1 个答案:

答案 0 :(得分:1)

可能错误的是您的关联定义:

Booking.belongsTo(models.Account, {foreignKey:'accId', targetKey:'accId'});

您的目标定义中没有属性accId,在本例中为Account表。

查看Target keys documentation

加成:

使用createdAt

时,您不需要声明updatedAttimestamps: true等属性