fixed bug when many to one entity in relation was not updating

This commit is contained in:
Umed Khudoiberdiev 2016-05-11 17:41:51 +05:00
parent d4cc792b95
commit a37dac3afc
5 changed files with 42 additions and 3 deletions

View File

@ -1,7 +1,7 @@
{
"name": "typeorm",
"private": true,
"version": "0.0.2-alpha.14",
"version": "0.0.2-alpha.15",
"description": "Data-mapper ORM for Typescript",
"license": "Apache-2.0",
"readmeFilename": "README.md",

View File

@ -24,9 +24,20 @@ createConnection(options).then(connection => {
let postRepository = connection.getRepository(Post);
let category1 = new Category();
category1.name = "category #1";
let category2 = new Category();
category2.name = "category #2";
let post = new Post();
post.text = "Hello how are you?";
post.title = "hello";
post.categories = [category1, category2];
let author = new Author();
author.name = "Umed";
post.author = author;
postRepository
.persist(post)
@ -40,6 +51,28 @@ createConnection(options).then(connection => {
})
.then(loadedPost => {
console.log("loadedPosts: ", loadedPost);
console.log("Lets update a post - add a new category and change author");
let category3 = new Category();
category3.name = "category #3";
post.categories.push(category3);
let author2 = new Author();
author2.name = "Bakhrom";
post.author = author2;
return postRepository.persist(post);
})
.then(updatedPost => {
return postRepository
.createQueryBuilder("post")
.leftJoinAndSelect("post.author", "author")
.leftJoinAndSelect("post.categories", "categories")
.where("post.id=:id", { id: post.id })
.getSingleResult();
})
.then(loadedPost => {
console.log(loadedPost);
})
.catch(error => console.log(error.stack));

View File

@ -1,8 +1,10 @@
import {PrimaryColumn, Column} from "../../../src/columns";
import {Table} from "../../../src/tables";
import {Category} from "./Category";
import {Author} from "./Author";
import {ManyToMany} from "../../../src/decorator/relations/ManyToMany";
import {JoinTable} from "../../../src/decorator/relations/JoinTable";
import {ManyToOne} from "../../../src/decorator/relations/ManyToOne";
@Table("sample23_post")
export class Post {
@ -22,4 +24,7 @@ export class Post {
@JoinTable()
categories: Category[];
@ManyToOne(type => Author, { cascadeAll: true })
author: Author;
}

View File

@ -149,7 +149,8 @@ export class EntityPersistOperationBuilder {
} else if (diff.length) {
const entityId = newEntity[metadata.primaryColumn.name];
operations.push(new UpdateOperation(newEntity, entityId, diff));
if (entityId)
operations.push(new UpdateOperation(newEntity, entityId, diff));
}
metadata.relations.forEach(relation => {

View File

@ -41,9 +41,9 @@ export class PersistOperationExecutor {
.then(() => this.executeUpdateTreeLevelOperations(persistOperation))
.then(() => this.executeInsertJunctionsOperations(persistOperation))
.then(() => this.executeRemoveJunctionsOperations(persistOperation))
.then(() => this.executeRemoveRelationOperations(persistOperation))
.then(() => this.executeUpdateRelationsOperations(persistOperation))
.then(() => this.executeUpdateOperations(persistOperation))
.then(() => this.executeRemoveRelationOperations(persistOperation))
.then(() => this.executeRemoveOperations(persistOperation))
.then(() => this.driver.endTransaction())
.then(() => this.updateIdsOfInsertedEntities(persistOperation))