-
-
Notifications
You must be signed in to change notification settings - Fork 502
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(query-builder): fix querying for a composite FK when target is jo…
…ined Closes #3738
- Loading branch information
Showing
2 changed files
with
94 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import { | ||
Collection, | ||
Entity, | ||
LoadStrategy, | ||
ManyToOne, | ||
OneToMany, OptionalProps, | ||
PrimaryKey, | ||
Property, | ||
} from '@mikro-orm/core'; | ||
import { MikroORM } from '@mikro-orm/postgresql'; | ||
import { randomUUID } from 'crypto'; | ||
|
||
@Entity() | ||
export class Question { | ||
|
||
[OptionalProps]?: 'createdAt'; | ||
|
||
@PrimaryKey({ type: 'uuid' }) | ||
id: string = randomUUID(); | ||
|
||
@PrimaryKey({ length: 6 }) | ||
createdAt: Date = new Date(); | ||
|
||
@OneToMany(() => Answer, answer => answer.question) | ||
answers: Collection<Answer> = new Collection<Answer>(this); | ||
|
||
@Property({ length: 255 }) | ||
name!: string; | ||
|
||
} | ||
|
||
@Entity() | ||
export class Answer { | ||
|
||
[OptionalProps]?: 'createdAt'; | ||
|
||
@PrimaryKey({ type: 'uuid' }) | ||
id: string = randomUUID(); | ||
|
||
@PrimaryKey({ length: 6 }) | ||
createdAt: Date = new Date(); | ||
|
||
@ManyToOne({ entity: () => Question }) | ||
question!: Question; | ||
|
||
} | ||
|
||
describe('GH issue 3738', () => { | ||
|
||
let orm: MikroORM; | ||
let question: Question; | ||
|
||
beforeAll(async () => { | ||
orm = await MikroORM.init({ | ||
entities: [Answer, Question], | ||
dbName: ':memory:', | ||
type: 'sqlite', | ||
loadStrategy: LoadStrategy.JOINED, | ||
}); | ||
await orm.schema.createSchema(); | ||
|
||
question = orm.em.create(Question, { answers: [{}], name: 'test question' }); | ||
await orm.em.flush(); | ||
}); | ||
|
||
afterAll(() => orm.close(true)); | ||
|
||
test('test with populate', async () => { | ||
const foundWithPopulate = await orm.em.find(Answer, { question }, { populate: ['question'] }); | ||
expect(foundWithPopulate[0]).toBeDefined(); | ||
}); | ||
|
||
test('test without populate', async () => { | ||
const foundWithoutPopulate = await orm.em.find(Answer, { question }); | ||
expect(foundWithoutPopulate[0]).toBeDefined(); | ||
}); | ||
|
||
test('test with query builder 1', async () => { | ||
const foundWithQb = await orm.em.createQueryBuilder(Answer).where({ question }).getResult(); | ||
expect(foundWithQb).toBeDefined(); | ||
await orm.em.populate(foundWithQb, ['question']); | ||
expect(foundWithQb[0].question).toBeDefined(); | ||
}); | ||
|
||
test('test with query builder 2', async () => { | ||
const foundWithQb = await orm.em.createQueryBuilder(Answer).leftJoin('question', 'q').where({ question }).getResult(); | ||
expect(foundWithQb).toBeDefined(); | ||
expect(foundWithQb[0].question).toBeDefined(); | ||
}); | ||
}); |