书架(knex) – belongsToMany关系不工作

我一直在尝试使用belongsToMany关系(Bookshelf)build立post和标签之间的关系。 这是我的代码:

db.js

const Post = bookshelf.Model.extend({ tableName: 'posts', hasTimestamps: true, tags: function(){ return this.belongsToMany(Tag) } }) const Tag = bookshelf.Model.extend({ tableName: 'tags', posts: function(){ return this.belongsToMany(Post) } }) // Pivot table const PostTag = bookshelf.Model.extend({ tableName: 'posts_tags', post: function(){ return this.belongsTo(Post) }, tag: function(){ return this.belongsTo(Tag) } }) 

获取路线是:

 .get('/:id', (req, res, next) => { db .Post .where('id', req.params.id) .fetch({widthRelated: ['tags'], require:true}) .then((data)=> { return res.json({data, ralation: data.related('tags').toJSON()}) }) }) 

我已经在数据库中添加了表'posts_tags',并且所有的数据库都包含了这个数据透视表。 所以当我在路由中查询时,关系查询甚至不会启动。 knex debug: sql:'select posts 。* from posts where id =? 限制?

posts – id标题文字created_at updated_at


标签 – ID名称created_at updated_at


posts_tags – id post_id tag_id created_at updated_at


代码中有没有错误?

对不起,这个post – 我只是错字:

 .fetch({widthRelated: ['tags'], require:true}) 

widthRelated = withRelated !!!!!!