羽毛续集:belongToMany或belongTo..创建关联



使用features+Sequalize(newbee(,我想为Tag模型建立一个多对多关系,其中Tag可以有很多父母和很多孩子。

tags.belongsToMany(tags, {
as: 'parents',
through: tags_tags,
foreignKey: 'parentId',
otherKey: 'id',
onDelete: 'RESTRICT',
onUpdate: 'CASCADE',
});
tags.belongsToMany(tags, {
as: 'children',
through: tags_tags,
foreignKey: 'id',
otherKey: 'parentId',
onDelete: 'CASCADE',
onUpdate: 'CASCADE',
});

模型似乎很好,数据库构建也很好。

现在,我正在研究如何在羽毛服务中添加关系。真的很沮丧,因为这应该很简单,但我似乎找不到任何能帮助我的东西。我是不是错过了一些显而易见的东西?

app.services.tags
.create({
name: 'siteRoot',
})
.then(siteRoot => {
// something like siteRoot.addChild() ?
// app.services.tags.Model has .children
// but how can I use it ?
})

在型号/标签中。型号.ts

// See http://docs.sequelizejs.com/en/latest/docs/models-definition/
// for more of what you can do here.
import { Sequelize, DataTypes, Op } from 'sequelize';
import { Application } from '../declarations';
export default function (app: Application) {
const sequelizeClient: Sequelize = app.get('sequelizeClient');
const tags = sequelizeClient.define(
'tags',
{
id: {
type: DataTypes.INTEGER,
allowNull: false,
primaryKey: true,
autoIncrement: true,
},
deleted: {
type: DataTypes.BOOLEAN,
allowNull: false,
defaultValue: false,
},
name: {
type: DataTypes.STRING,
allowNull: false,
},
},
{
hooks: {
beforeCount(options: any) {
options.raw = true;
},
},
// timestamps: false,
// tableName: 'tag',
// underscored: true,
indexes: [
{
fields: ['name'],
},
}
);
// eslint-disable-next-line no-unused-vars
(tags as any).associate = function (models: any) {
// Define associations here
// See http://docs.sequelizejs.com/en/latest/docs/associations/
const { tags } = models;
tags.belongsTo(tags, {
foreignKey: 'siteBaseTagId',
as: 'siteBaseTag',
onDelete: 'RESTRICT',
onUpdate: 'CASCADE',
});
tags.hasMany(tags, {
foreignKey: 'siteBaseTagId',
as: 'siteTags',
});
};
return tags;
}

和型号/标签标签型号.ts


// See http://docs.sequelizejs.com/en/latest/docs/models-definition/
// for more of what you can do here.
import { Sequelize, DataTypes } from 'sequelize';
import { Application } from '../declarations';
export default function (app: Application) {
const sequelizeClient: Sequelize = app.get('sequelizeClient');
const tagsTags = sequelizeClient.define(
'tags_tags',
{
id: {
type: DataTypes.INTEGER,
allowNull: false,
primaryKey: true,
},
parentId: {
type: DataTypes.INTEGER,
allowNull: false,
primaryKey: true,
},
template: {
type: DataTypes.STRING,
allowNull: true,
},
url: {
type: DataTypes.STRING,
allowNull: true,
},
},
{
hooks: {
beforeCount(options: any) {
options.raw = true;
},
},
timestamps: false,
}
);
// eslint-disable-next-line no-unused-vars
(tagsTags as any).associate = function (models: any) {
// Define associations here
// See http://docs.sequelizejs.com/en/latest/docs/associations/
const { tags, tags_tags } = models;
tags.belongsToMany(tags, {
as: 'parents',
through: tags_tags,
foreignKey: 'parentId',
otherKey: 'id',
onDelete: 'RESTRICT',
onUpdate: 'CASCADE',
});
tags.belongsToMany(tags, {
as: 'children',
through: tags_tags,
foreignKey: 'id',
otherKey: 'parentId',
onDelete: 'CASCADE',
onUpdate: 'CASCADE',
});
tags_tags.belongsTo(tags, {
foreignKey: 'parentId',
});
tags.hasMany(tags_tags, {
foreignKey: 'parentId',
});
tags_tags.belongsTo(tags, {
foreignKey: 'id',
});
tags.hasMany(tags_tags, {
foreignKey: 'id',
});
};
return tagsTags;
}

belongsToMany是羽毛方面最难处理的关联之一。限制绝对不在feathers端,而在sequelize如何处理它们。

我只是对你的问题有点困惑。如果你能确切地说明哪里需要帮助,我愿意提供帮助。

旁注:也许你应该对你的模型进行一点不同的重命名。tags||tags_tags似乎彼此接近,并且可能会在某个时候混淆。Mean time喜欢将您指向这个已经活动了一段时间的线程,因为每个人都在以不同的方式处理belongsToMany关系。希望你能从中得到一些启示。https://github.com/feathersjs/feathers/issues/852#issuecomment-406413342

如果你只需要M:N,那么我只需要这样做:

// tag model
tag.associate = models => {
tag.belongsToMany(models.tag_tag, {
through: 'parent_child_table', // name this table to your liking.
foreignKey: 'tag_id'
});
};
// tag_tag model
tag_tag.associate = models => {
tag_tag.belongsToMany(models.tag, {
through: 'parent_child_table', // table name MUST be the same as above
foreignKey: 'tag_tag_id'
});
};

以上内容将创建另一个表parent_child_table,您将在其中跟踪您的关联。您必须创建一个单独的服务才能在此表上执行CRUD。这应该会让你开始。

相关内容

  • 没有找到相关文章

最新更新