Repository navigation
Closed
Description
Type
[x] mysql
[x] bug report
[x] 0.1.16, 0.2.0-alpha.13 and 0.2.0-alpha.46
Description
If you specify a clause for a property on a relation, that clause gets ignored and ORM instead adds a where clause on the join column against NULL
Reproducing
Simply edit the connection details in the below code to point to an empty mysql database and run it in an environment with TypeORM available. I used ts-node to run if that makes any difference.
/* tslint:disable */
import { BaseEntity, Connection, createConnection, Entity, PrimaryGeneratedColumn, ManyToOne, OneToMany, Column } from "typeorm";
@Entity("threads")
class Thread extends BaseEntity {
@PrimaryGeneratedColumn()
public id: number;
@Column("varchar")
public name: string;
@OneToMany((type) => Post, (comments) => comments.thread)
public posts: Post[];
}
@Entity("posts")
class Post extends BaseEntity {
@PrimaryGeneratedColumn()
public id: number;
@ManyToOne((type) => Thread, (thread) => thread.posts)
public thread: Thread;
}
(async () => {
let dbConn: Connection;
try {
dbConn = await createConnection({
type: "mysql",
host: "127.0.0.1",
port: 3306,
database: "database",
password: "123",
username: "root",
synchronize: true,
logging: true,
entities: [Thread, Post],
});
const threadName = "Mega Thread";
const thread = await Thread.create({ name: threadName }).save();
const comment = await Post.create({ thread }).save();
const test = await Post.find({
relations: ["thread"],
where: {
thread: {
name: threadName,
},
},
});
console.log(test);
} catch (error) {
console.error(error.message);
} finally {
if (dbConn) {
dbConn.close();
}
}
})();The above gives me the following SQL with logging enabled:
SELECT
`Post`.`id` AS `Post_id`,
`Post`.`threadId` AS `Post_threadId`,
`Post_thread`.`id` AS `Post_thread_id`,
`Post_thread`.`name` AS `Post_thread_name`
FROM `posts` `Post`
LEFT JOIN `threads` `Post_thread` ON `Post_thread`.`id`=`Post`.`threadId`
WHERE `Post`.`threadId` = ? -- PARAMETERS: [null]I expected to see this however:
SELECT
`Post`.`id` AS `Post_id`,
`Post`.`threadId` AS `Post_threadId`,
`Post_thread`.`id` AS `Post_thread_id`,
`Post_thread`.`name` AS `Post_thread_name`
FROM `posts` `Post`
LEFT JOIN `threads` `Post_thread` ON `Post_thread`.`id`=`Post`.`threadId`
- WHERE `Post`.`threadId` = ? -- PARAMETERS: [null]
+ WHERE `Post_thread`.`name` = ? -- PARAMETERS: ["Mega Thread"]Reacted by Loïc Poullain, Daniel Andújar, khalidhoffman, Jean-Sébastien Herbaux, Martin Kleinschrodt, Diede Exterkate, Lukas Hamrla, Rishinder Rana, ALT, Matthias Kuklinski and 2 more
Activity
pleerock commentedon Mar 28, 2018
can you try it in
@next? npm i typeorm@nextSlessi commentedon Mar 29, 2018
With
0.2.0-alpha.13Still attempts
It is easily reproduced if you wish, just need to run the code I attached to OP with a connection to any existing mysql DB
pleerock commentedon Apr 17, 2018
alpha.13is very old, please check it on latest alpha.Slessi commentedon Apr 17, 2018
Used
typeorm@nextagain which gives (at time of posting)0.2.0-alpha.46and the problem still persists.Problem occurs on
0.1.16,0.2.0-alpha.13and0.2.0-alpha.46.I see that
alpha.13is now old but I lasted updated 19 days ago at which time it was the latest.pleerock commentedon Apr 17, 2018
nope, it wasn't latest. alpha 13 is kinda 5 months old.
I see your issue now, you cannot request by non-primary column your relation via find-options. You need to use query builder instead.
Slessi commentedon Apr 17, 2018
Can we call it a feature request then? :p
pleerock commentedon Apr 17, 2018
I'll put it on the milestone and will think how we can implement it
gado704555 commentedon May 8, 2018
I have some question , Is possible to order by relations column like this ?
const test = await Post.find({ relations: ["thread"], order: { thread: { name: 'DESC' } } });owengombas commentedon Oct 19, 2018
I have the same issue, is it implemented in the branch @next ? @pleerock
owengombas commentedon Oct 19, 2018
So I've made a parser to make nested relations with where conditions
the first composeQuery argument is the Model (it extends BaseEntity and it's decorated with @entity()) the second parameter is an object the where rules