Skip to content

Instantly share code, notes, and snippets.

@IlyaSemenov
Created October 23, 2017 09:15
Show Gist options
  • Save IlyaSemenov/f4a81b5e1006fd5fb396f2d17facf2b9 to your computer and use it in GitHub Desktop.
Save IlyaSemenov/f4a81b5e1006fd5fb396f2d17facf2b9 to your computer and use it in GitHub Desktop.
Demonstrate Objection.js crash on select(1) in eager join
const Knex = require('knex')
const objection = require('objection')
const Model = objection.Model
const knex = Knex({ client: 'pg', connection: { database: 'test' } })
Model.knex(knex)
class Foo extends Model {
static get tableName () {
return 'foo'
}
static get relationMappings () {
return {
bar: {
relation: Model.HasOneRelation, // reverse link
modelClass: Bar,
join: {
from: 'bar.foo_id',
to: 'foo.id',
},
},
}
}
}
class Bar extends Model {
static get tableName () {
return 'bar'
}
static get idColumn () {
return 'foo_id'
}
}
async function main () {
await knex.schema.createTable('foo', table => {
table.integer('id').primary()
table.string('name')
})
await knex.schema.createTable('bar', table => {
table.integer('foo_id').primary()
table.foreign('foo_id').references('foo.id')
})
await Foo.query().insert({id: 1, name: 'one'})
await Foo.query().insert({id: 2, name: 'two'})
await Bar.query().insert({foo_id: 2})
const foosWithBar = await Foo.query()
.eagerAlgorithm(Foo.JoinEagerAlgorithm)
.eager('bar(bar)', {
bar: query => query.select(1),
})
.whereNotNull('bar.foo_id')
console.log('Foos with bar:', foosWithBar)
}
main().then(() => { console.log('finished') }).catch(err => { console.error(err) })
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment