Skip to content

Commit

Permalink
fix(core): ensure virtual relation properties have no effect on commi…
Browse files Browse the repository at this point in the history
…t order

Closes #4781
  • Loading branch information
B4nan committed Oct 4, 2023
1 parent 7e08037 commit 606d633
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/core/src/unit-of-work/CommitOrderCalculator.ts
Expand Up @@ -72,7 +72,7 @@ export class CommitOrderCalculator {
return;
}

this.addDependency(propertyType, entityName, prop.nullable ? 0 : 1);
this.addDependency(propertyType, entityName, prop.nullable || prop.persist === false ? 0 : 1);
}

/**
Expand Down
99 changes: 99 additions & 0 deletions tests/issues/GH4781.test.ts
@@ -0,0 +1,99 @@
import {
Collection,
DateType,
Entity,
ManyToOne,
OneToMany,
OneToOne,
PrimaryKey,
Property,
Ref,
ref,
} from '@mikro-orm/core';
import { MikroORM } from '@mikro-orm/sqlite';
import { v4 } from 'uuid';

@Entity()
class Author {

@PrimaryKey()
id = v4();

@Property()
name!: string;

@OneToMany({
entity: () => Book,
mappedBy: 'author',
})
books = new Collection<Book>(this);

@OneToOne({
entity: () => Book,
ref: true,
formula: alias =>
`(select "b"."id"
from (
select "b"."author_id", min("b"."release_date") "release_date"
from "book" "b"
where "b"."author_id" = ${alias}."id"
group by "b"."author_id"
) "s1"
join "book" "b"
on "b"."author_id" = "s1"."author_id"
and "b"."release_date" = "s1"."release_date")`,
})
firstBook!: Ref<Book>;

constructor(name: string) {
this.name = name;
const myFirstBook = new Book(new Date(), 'My first book', this);
this.books.add(myFirstBook);
this.firstBook = ref(myFirstBook);
}

}

@Entity()
class Book {

@PrimaryKey()
id = v4();

@Property({ type: DateType })
releaseDate!: Date;

@Property()
name!: string;

@ManyToOne({
entity: () => Author,
ref: true,
inversedBy: 'books',
})
author!: Ref<Author>;

constructor(releaseDate: Date = new Date(), name: string, author: Author) {
this.releaseDate = releaseDate;
this.name = name;
this.author = ref(author);
}

}

let orm: MikroORM;

beforeAll(async () => {
orm = await MikroORM.init({
entities: [Author, Book],
dbName: ':memory:',
});
await orm.schema.createSchema();
});

afterAll(() => orm.close(true));

test(`GH issue 1079`, async () => {
const author = new Author('John');
await orm.em.persistAndFlush(author);
});

0 comments on commit 606d633

Please sign in to comment.