admin管理员组文章数量:1315831
I have this many to many relation in Postgres:
// migrations/2020_create_initial_tables.js
exports.up = function(knex) {
return knex.schema
.createTable('students', function(table) {
table.increments('id').primary()
table
.string('email')
.unique()
.index()
table.string('password')
})
.createTable('courses', function(table) {
table.increments('id').primary()
table.string('title').notNullable()
table.text('description')
})
// A student can enroll many courses
// A course can have many students
.createTable('student_courses', function(table) {
table.increments('id').primary()
table
.integer('student_id')
.references('id')
.inTable('students')
table
.integer('course_id')
.references('id')
.inTable('courses')
})
.catch(err => {
console.error(err)
throw err
})
// .finally(() => knex.destroy());
}
exports.down = function(knex) {
return knex.schema
.dropTableIfExists('students')
.dropTableIfExists('courses')
.dropTableIfExists('student_courses')
.catch(err => {
console.error(err)
throw err
})
}
I need to show a student's enrolled courses.
How do I query (all/an array of) courses
by student.id
?
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Stack: TypeScript, [email protected], Postgres@12-alpine, [email protected]
I have this many to many relation in Postgres:
// migrations/2020_create_initial_tables.js
exports.up = function(knex) {
return knex.schema
.createTable('students', function(table) {
table.increments('id').primary()
table
.string('email')
.unique()
.index()
table.string('password')
})
.createTable('courses', function(table) {
table.increments('id').primary()
table.string('title').notNullable()
table.text('description')
})
// A student can enroll many courses
// A course can have many students
.createTable('student_courses', function(table) {
table.increments('id').primary()
table
.integer('student_id')
.references('id')
.inTable('students')
table
.integer('course_id')
.references('id')
.inTable('courses')
})
.catch(err => {
console.error(err)
throw err
})
// .finally(() => knex.destroy());
}
exports.down = function(knex) {
return knex.schema
.dropTableIfExists('students')
.dropTableIfExists('courses')
.dropTableIfExists('student_courses')
.catch(err => {
console.error(err)
throw err
})
}
I need to show a student's enrolled courses.
How do I query (all/an array of) courses
by student.id
?
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Stack: TypeScript, [email protected], Postgres@12-alpine, [email protected]
Share Improve this question asked Apr 8, 2020 at 3:25 SwixSwix 2,12311 gold badges38 silver badges57 bronze badges1 Answer
Reset to default 8const coursesOfSingleStudent = await knex('courses').whereIn('id',
knex('student_courses').select('course_id').where('student_id', studentId)
)
Though you might be better off using objection.js which allows you to declare relation mappings and then query directly:
const studentWithCourses = await Student.query().findById(studentId).withGraphFetched('courses');
本文标签: javascriptHow to query many to many relations with KnexjsStack Overflow
版权声明:本文标题:javascript - How to query many to many relations with Knex.js? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741987981a2408798.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论