mirror of
https://github.com/typeorm/typeorm.git
synced 2025-12-08 21:26:23 +00:00
fixed issue in join without relation
This commit is contained in:
parent
76313703a0
commit
eff6a2d3ef
@ -1,7 +1,7 @@
|
||||
{
|
||||
"name": "typeorm",
|
||||
"private": true,
|
||||
"version": "0.0.2-alpha.11",
|
||||
"version": "0.0.2-alpha.12",
|
||||
"description": "Data-mapper ORM for Typescript",
|
||||
"license": "Apache-2.0",
|
||||
"readmeFilename": "README.md",
|
||||
|
||||
@ -22,6 +22,8 @@ const options: CreateConnectionOptions = {
|
||||
|
||||
createConnection(options).then(connection => {
|
||||
|
||||
let entityManager = connection.entityManager;
|
||||
|
||||
let postRepository = connection.getRepository(Post);
|
||||
let authorRepository = connection.getRepository(Author);
|
||||
let categoryRepository = connection.getRepository(Category);
|
||||
@ -38,24 +40,24 @@ createConnection(options).then(connection => {
|
||||
let post = postRepository.create();
|
||||
post.text = "Hello how are you?";
|
||||
post.title = "hello";
|
||||
post.author = author;
|
||||
post.categories = [category1, category2];
|
||||
post.authorId = 1;
|
||||
// post.author = author;
|
||||
// post.categories = [category1, category2];
|
||||
|
||||
postRepository
|
||||
.persist(post)
|
||||
.then(post => {
|
||||
console.log("Post has been saved.");
|
||||
console.log(post);
|
||||
|
||||
console.log("Now lets load posts ");
|
||||
/*return postRepository.find({
|
||||
alias: "post",
|
||||
leftJoinAndSelect: {
|
||||
author: "post.author",
|
||||
metadata: "post.metadata",
|
||||
categories: "post.categories"
|
||||
}
|
||||
});*/
|
||||
Promise.all<any>([
|
||||
postRepository.persist(post),
|
||||
authorRepository.persist(author),
|
||||
categoryRepository.persist(category1),
|
||||
categoryRepository.persist(category2),
|
||||
])
|
||||
.then(() => {
|
||||
console.log("Everything has been saved.");
|
||||
})
|
||||
.then(() => {
|
||||
return postRepository
|
||||
.createQueryBuilder("post")
|
||||
.leftJoin(Author, "author", "ON", "author.id=post.authorId")
|
||||
.getResults();
|
||||
|
||||
// let secondPost = postRepository.create();
|
||||
// secondPost.text = "Second post";
|
||||
@ -64,6 +66,13 @@ createConnection(options).then(connection => {
|
||||
|
||||
}).then(post => {
|
||||
console.log("Loaded posts: ", post);
|
||||
|
||||
return entityManager
|
||||
.createQueryBuilder(Author, "author")
|
||||
.getResults();
|
||||
|
||||
}).then(authors => {
|
||||
console.log("Loaded authors: ", authors);
|
||||
})
|
||||
/* posts[0].title = "should be updated second post";
|
||||
|
||||
|
||||
@ -9,8 +9,5 @@ export class Author {
|
||||
|
||||
@Column()
|
||||
name: string;
|
||||
|
||||
@Column("int")
|
||||
postId: number;
|
||||
|
||||
}
|
||||
@ -10,7 +10,4 @@ export class Category {
|
||||
@Column()
|
||||
name: string;
|
||||
|
||||
@Column("int")
|
||||
postId: number;
|
||||
|
||||
}
|
||||
@ -6,7 +6,7 @@ import {Category} from "./Category";
|
||||
@Table("sample20_post")
|
||||
export class Post {
|
||||
|
||||
@PrimaryColumn("int")
|
||||
@PrimaryColumn("int", { generated: true })
|
||||
id: number;
|
||||
|
||||
@Column()
|
||||
@ -15,8 +15,7 @@ export class Post {
|
||||
@Column()
|
||||
text: string;
|
||||
|
||||
author: Author;
|
||||
|
||||
categories: Category[];
|
||||
@Column("int")
|
||||
authorId: number;
|
||||
|
||||
}
|
||||
@ -569,15 +569,17 @@ export class QueryBuilder<Entity> {
|
||||
protected createJoinExpression() {
|
||||
return this.joins.map(join => {
|
||||
const joinType = join.type; // === "INNER" ? "INNER" : "LEFT";
|
||||
const appendedCondition = join.condition ? " AND " + join.condition : "";
|
||||
const parentAlias = join.alias.parentAliasName;
|
||||
const parentMetadata = this.aliasMap.getEntityMetadataByAlias(this.aliasMap.findAliasByName(parentAlias));
|
||||
const parentTable = parentMetadata.table.name;
|
||||
const parentTableColumn = parentMetadata.primaryColumn.name;
|
||||
const relation = parentMetadata.findRelationWithDbName(join.alias.parentPropertyName);
|
||||
const junctionMetadata = relation.junctionEntityMetadata;
|
||||
const joinMetadata = this.aliasMap.getEntityMetadataByAlias(join.alias);
|
||||
const joinTableName = joinMetadata.table.name;
|
||||
const parentAlias = join.alias.parentAliasName;
|
||||
if (!parentAlias) {
|
||||
return " " + joinType + " JOIN " + joinTableName + " " + join.alias.name + " " + join.conditionType + " " + join.condition;
|
||||
}
|
||||
|
||||
const parentMetadata = this.aliasMap.getEntityMetadataByAlias(this.aliasMap.findAliasByName(parentAlias));
|
||||
const relation = parentMetadata.findRelationWithDbName(join.alias.parentPropertyName);
|
||||
const junctionMetadata = relation.junctionEntityMetadata;
|
||||
const appendedCondition = join.condition ? " AND " + join.condition : "";
|
||||
|
||||
if (relation.isManyToMany) {
|
||||
const junctionTable = junctionMetadata.table.name;
|
||||
@ -603,7 +605,7 @@ export class QueryBuilder<Entity> {
|
||||
return " " + joinType + " JOIN " + joinTableName + " " + join.alias.name + " " + join.conditionType + " " + condition + appendedCondition;
|
||||
|
||||
} else {
|
||||
return " " + joinType + " JOIN " + joinTableName + " " + join.alias.name + " " + join.conditionType + " " + join.condition;
|
||||
throw new Error("Unexpected relation type"); // this should not be possible
|
||||
}
|
||||
}).join(" ");
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user