Sails 中 model 里的外键字段的设置问题
发布于 6 年前 作者 rwing 2480 次浏览 来自 问答

以官网的文档为例,似乎不可以在 pet.js 里再添加一个 userId 的外键 attribute?但是我只想获取到这个外键怎么办,不要需要把关联表所有数据都拿出来。

// myApp/api/models/User.js
// A user may have many pets
module.exports = {
  attributes: {
    firstName: {
      type: 'string'
    },
    lastName: {
      type: 'string'
    },

    // Add a reference to Pets
    pets: {
      collection: 'pet',
      via: 'owner'
    }
  }
};
// myApp/api/models/Pet.js
// A pet may only belong to a single user
module.exports = {
  attributes: {
    breed: {
      type: 'string'
    },
    type: {
      type: 'string'
    },
    name: {
      type: 'string'
    },

    // Add a reference to User
    owner: {
      model: 'user',
	  columnName: 'user_id'
    }
	// 这里再加一个 userId 是不行的,会提示和上面 owner 公用了一个 columnName
	userId: {
	     columnName: 'user_id',
	}
  }
};
回到顶部