Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/delphi/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 书架(knex)-所属关系不起作用_Mysql_Node.js_Express_Bookshelf.js_Knex.js - Fatal编程技术网

Mysql 书架(knex)-所属关系不起作用

Mysql 书架(knex)-所属关系不起作用,mysql,node.js,express,bookshelf.js,knex.js,Mysql,Node.js,Express,Bookshelf.js,Knex.js,我一直在尝试使用belongtomany关系(Bookshelf)在帖子和标签之间建立关系。这是我的密码: db.js const Post = bookshelf.Model.extend({ tableName: 'posts', hasTimestamps: true, tags: function(){ return this.belongsToMany(Tag) } }) const Tag = bookshelf.Model.exte

我一直在尝试使用belongtomany关系(Bookshelf)在帖子和标签之间建立关系。这是我的密码:

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',所有数据库都是种子,包括这个pivot表。因此,当我在route中查询时,关系查询甚至不会启动。knex debug:sql:“从
posts
中选择
posts
*,其中
id
=”?限制?“

表格

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)
    }

})
帖子-id标题文本在更新时创建


标记-在更新时创建的id名称


posts_tags-id post_id tag_id已在更新时创建



代码中有错误吗?

很抱歉,我刚才输入了错误:

.fetch({widthRelated: ['tags'], require:true})
宽度相关=与相关