使用羽毛+ sequelize(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 ?
})
在models / tags.model.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;
}
和在models / tags-tags.model.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
如何处理它们。
我对您的问题有点困惑。如果您可以确切说明需要帮助的地方,则希望提供帮助。
旁注:也许您应该将模型重命名为diff。tags
|| tags_tags
似乎彼此接近,并且//在某些时候可能会造成混淆。平均时间喜欢将您指向该线程,该线程已经活跃了一段时间,因为每个人对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
。这应该可以帮助您入门。