mirror of
https://github.com/typeorm/typeorm.git
synced 2025-12-08 21:26:23 +00:00
added tests and fix another bug
This commit is contained in:
parent
54274b1966
commit
8db90339fa
@ -111,7 +111,7 @@ export class EntityPersister<Entity extends ObjectLiteral> {
|
||||
protected async findNotLoadedIds(persistedEntities: OperateEntity[], dbEntities?: OperateEntity[]): Promise<OperateEntity[]> {
|
||||
const newDbEntities: OperateEntity[] = dbEntities ? dbEntities.map(dbEntity => dbEntity) : [];
|
||||
const missingDbEntitiesLoad = persistedEntities.map(async entityWithId => {
|
||||
|
||||
console.log(entityWithId);
|
||||
if (entityWithId.id === null || // todo: not sure if this condition will work
|
||||
entityWithId.id === undefined || // todo: not sure if this condition will work
|
||||
newDbEntities.find(dbEntity => dbEntity.entityTarget === entityWithId.entityTarget && dbEntity.compareId(entityWithId.id!)))
|
||||
@ -121,19 +121,20 @@ export class EntityPersister<Entity extends ObjectLiteral> {
|
||||
const parameters: ObjectLiteral = {};
|
||||
let condition = "";
|
||||
|
||||
if (this.metadata.hasParentIdColumn) {
|
||||
condition = this.metadata.parentIdColumns.map(parentIdColumn => {
|
||||
const metadata = this.connection.entityMetadatas.findByTarget(entityWithId.entityTarget);
|
||||
|
||||
if (metadata.hasParentIdColumn) {
|
||||
condition = metadata.parentIdColumns.map(parentIdColumn => {
|
||||
parameters[parentIdColumn.propertyName] = entityWithId.id![parentIdColumn.propertyName];
|
||||
return alias + "." + parentIdColumn.propertyName + "=:" + parentIdColumn.propertyName;
|
||||
}).join(" AND ");
|
||||
} else {
|
||||
condition = this.metadata.primaryColumns.map(primaryColumn => {
|
||||
condition = metadata.primaryColumns.map(primaryColumn => {
|
||||
parameters[primaryColumn.propertyName] = entityWithId.id![primaryColumn.propertyName];
|
||||
return alias + "." + primaryColumn.propertyName + "=:" + primaryColumn.propertyName;
|
||||
}).join(" AND ");
|
||||
}
|
||||
|
||||
const metadata = this.connection.entityMetadatas.findByTarget(entityWithId.entityTarget);
|
||||
const loadedEntity = await new QueryBuilder(this.connection, this.queryRunner)
|
||||
.select(alias)
|
||||
.from(entityWithId.entityTarget, alias)
|
||||
|
||||
@ -0,0 +1,21 @@
|
||||
import {Table} from "../../../../../src/decorator/tables/Table";
|
||||
import {PrimaryColumn} from "../../../../../src/decorator/columns/PrimaryColumn";
|
||||
import {Column} from "../../../../../src/decorator/columns/Column";
|
||||
import {Post} from "./Post";
|
||||
import {OneToMany} from "../../../../../src/decorator/relations/OneToMany";
|
||||
import {JoinColumn} from "../../../../../src/decorator/relations/JoinColumn";
|
||||
|
||||
|
||||
@Table()
|
||||
export class Category {
|
||||
|
||||
@PrimaryColumn("int", {generated: true})
|
||||
categoryId: number;
|
||||
|
||||
@Column()
|
||||
name: string;
|
||||
|
||||
@OneToMany(type => Post, post => post.category)
|
||||
posts: Post[];
|
||||
|
||||
}
|
||||
23
test/functional/persistence/multi-primary-key/entity/Post.ts
Normal file
23
test/functional/persistence/multi-primary-key/entity/Post.ts
Normal file
@ -0,0 +1,23 @@
|
||||
import {Table} from "../../../../../src/decorator/tables/Table";
|
||||
import {PrimaryColumn} from "../../../../../src/decorator/columns/PrimaryColumn";
|
||||
import {Column} from "../../../../../src/decorator/columns/Column";
|
||||
import {ManyToOne} from "../../../../../src/decorator/relations/ManyToOne";
|
||||
import {JoinColumn} from "../../../../../src/decorator/relations/JoinColumn";
|
||||
import {Category} from "./Category";
|
||||
|
||||
@Table()
|
||||
export class Post {
|
||||
|
||||
@PrimaryColumn("int")
|
||||
firstId: number;
|
||||
|
||||
@PrimaryColumn("int")
|
||||
secondId: number;
|
||||
|
||||
@Column()
|
||||
title: string;
|
||||
|
||||
@ManyToOne(type => Category, category => category.posts)
|
||||
category: Category;
|
||||
|
||||
}
|
||||
@ -0,0 +1,57 @@
|
||||
import "reflect-metadata";
|
||||
import {setupTestingConnections, closeConnections, reloadDatabases} from "../../../utils/test-utils";
|
||||
import {Connection} from "../../../../src/connection/Connection";
|
||||
import {Post} from "./entity/Post";
|
||||
import {Category} from "./entity/Category";
|
||||
|
||||
describe("persistence > mutli primary keys", () => {
|
||||
|
||||
let connections: Connection[];
|
||||
before(async () => connections = await setupTestingConnections({
|
||||
entities: [__dirname + "/entity/*{.js,.ts}"],
|
||||
schemaCreate: true,
|
||||
dropSchemaOnConnection: true
|
||||
}));
|
||||
beforeEach(() => reloadDatabases(connections));
|
||||
after(() => closeConnections(connections));
|
||||
|
||||
describe("insertt", function () {
|
||||
|
||||
it("should insert entity when when there are mutli column primary keys", () => Promise.all(connections.map(async connection => {
|
||||
const post1 = new Post();
|
||||
post1.title = "Hello Post #1";
|
||||
post1.firstId = 1;
|
||||
post1.secondId = 2;
|
||||
|
||||
await connection.entityManager.persist(post1);
|
||||
|
||||
// create first category and post and save them
|
||||
const category1 = new Category();
|
||||
category1.name = "Category saved by cascades #1";
|
||||
category1.posts = [post1];
|
||||
|
||||
await connection.entityManager.persist(category1);
|
||||
|
||||
// now check
|
||||
const posts = await connection.entityManager.find(Post, {
|
||||
alias: "post",
|
||||
innerJoinAndSelect: {
|
||||
category: "post.category"
|
||||
},
|
||||
orderBy: {
|
||||
"post.firstId": "ASC"
|
||||
}
|
||||
});
|
||||
|
||||
posts.should.be.eql([{
|
||||
firstId: 1,
|
||||
secondId: 2,
|
||||
title: "Hello Post #1",
|
||||
category: {
|
||||
categoryId: 1,
|
||||
name: "Category saved by cascades #1"
|
||||
}
|
||||
}]);
|
||||
})));
|
||||
});
|
||||
});
|
||||
Loading…
x
Reference in New Issue
Block a user