归结关联方法属性

时间:2018-08-16 13:09:46

标签: javascript sql database sequelize.js

我在使用关联方法时遇到困难。我有许多关联的用户和列表,因此续集会为我生成方法user.getLists();。但是当我称这种方法为

[
    {
        "id": 1,
        "title": "Boop",
        "created_at": "2018-08-15T14:53:51.276Z",
        "updated_at": "2018-08-15T14:53:51.276Z",
        "deleted_at": null,
        "user_list": {
            "owner": true,
            "created_at": "2018-08-15T14:53:51.290Z",
            "updated_at": "2018-08-15T14:53:51.290Z",
            "deleted_at": null,
            "user_id": 1,
            "list_id": 1
        }
    }
]

我找不到从其中删除user_list属性或重命名属性的方法。 有没有办法在ORM级别上删除此属性或对其重命名?

例如,这不起作用:

user.getLists({ attributes: ['id'] });
user.getLists({ attributes: [['user_list', 'random_name']] });

我的模特看起来像这样

用户模型:

const user = db.define(
  'user',
  {
    id: {
      type: sequelize.INTEGER,
      primaryKey: true,
      autoIncrement: true,
    },
    email: {
      type: sequelize.STRING,
      unique: 'email_unique',
      allowNull: false,
      validate: {
        isEmail: true,
      },
    },
});

列表模型:

const list = db.define(
  'list',
  {
    id: {
      type: sequelize.INTEGER,
      primaryKey: true,
      autoIncrement: true,
    },
    title: {
      type: sequelize.STRING,
      allowNull: false,
    },
  },
);

用户列表:

const userList = db.define(
  'user_list',
  {
    owner: {
      type: sequelize.BOOLEAN,
      allowNull: false,
    },
  },
);

协会:

User.belongsToMany(
    List,
    {
      through: UserList,
      as: {
        singular: 'List',
        plural: 'Lists',
      },
    })

List.belongsToMany(User, { through: UserList });

0 个答案:

没有答案