Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/399.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/5/sql/76.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
Javascript 如何使用Knex.js查询多对多关系?_Javascript_Sql_Node.js_Postgresql_Knex.js - Fatal编程技术网

Javascript 如何使用Knex.js查询多对多关系?

Javascript 如何使用Knex.js查询多对多关系?,javascript,sql,node.js,postgresql,knex.js,Javascript,Sql,Node.js,Postgresql,Knex.js,我在研究生中有这样一种多对多的关系: //migrations/2020\u create\u initial\u tables.js exports.up=函数(knex){ 返回knex.schema .createTable('students',函数(表){ 表.增量('id').primary() 桌子 .string('电子邮件') .unique() .index() table.string('password') }) .createTable('courses',函数(表){

我在研究生中有这样一种多对多的关系:

//migrations/2020\u create\u initial\u tables.js
exports.up=函数(knex){
返回knex.schema
.createTable('students',函数(表){
表.增量('id').primary()
桌子
.string('电子邮件')
.unique()
.index()
table.string('password')
})
.createTable('courses',函数(表){
表.增量('id').primary()
table.string('title').notNullable()
表.文本('说明')
})
//一个学生可以选修许多课程
//一门课程可以有很多学生
.createTable('student_courses',函数(表){
表.增量('id').primary()
桌子
.integer('student_id')
.references('id'))
.inTable('学生')
桌子
.integer('course\u id')
.references('id'))
.inTable(“课程”)
})
.catch(错误=>{
控制台错误(err)
失误
})
//.最后(()=>knex.destroy());
}
exports.down=函数(knex){
返回knex.schema
.dropTableIfExists(“学生”)
.dropTableIfExists(“课程”)
.dropTableIfExists(“学生课程”)
.catch(错误=>{
控制台错误(err)
失误
})
}
我需要出示学生注册的课程。 如何通过
student.id
查询(全部/一系列)
课程

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

堆栈:TypeScript,knex@v0.20.12, Postgres@12-阿尔卑斯山,pg@v7.18.2

const coursesOfSingleStudent = await knex('courses').whereIn('id',
   knex('student_courses').select('course_id').where('student_id', studentId)
)
尽管您最好使用Objective.js,它允许您声明关系映射,然后直接查询:

const studentWithCourses = await Student.query().findById(studentId).withGraphFetched('courses');
尽管您最好使用Objective.js,它允许您声明关系映射,然后直接查询:

const studentWithCourses = await Student.query().findById(studentId).withGraphFetched('courses');

很好的建议,但实际上没有关于如何(例如)为m-to-m关系创建适当迁移的文档。一般来说,knex&Objective的文档不是很好,说得委婉一点。您是否有一个很好的例子来说明如何通过迁移创建适当的m-to-m关系,然后将其用于反对,或者根据反对模型生成迁移?与使用SQL进行迁移的方法相同。。。如果你的意思是如何创建表和外键等,你需要添加一个关于它的单独问题(或者只是从中搜索答案)。无法基于数据库模型生成迁移。与JS world的标准相比,Objective实际上有相当好的文档。。。knex没有那么多。这是一个很好的建议,但实际上没有关于如何为m-to-m关系创建适当迁移的文档。一般来说,knex&Objective的文档不是很好,说得委婉一点。您是否有一个很好的例子来说明如何通过迁移创建适当的m-to-m关系,然后将其用于反对,或者根据反对模型生成迁移?与使用SQL进行迁移的方法相同。。。如果你的意思是如何创建表和外键等,你需要添加一个关于它的单独问题(或者只是从中搜索答案)。无法基于数据库模型生成迁移。与JS world的标准相比,Objective实际上有相当好的文档。。。不太可能。