Find displays a different order than the database
Issue type:
[x] question [ ] bug report [ ] feature request [ ] documentation issue
Database system/driver:
[ ] cordova
[ ] mongodb
[ ] mssql
[ ] mysql / mariadb
[ ] oracle
[x] postgres
[ ] cockroachdb
[ ] sqlite
[ ] sqljs
[ ] react-native
[ ] expo
TypeORM version:
[ ] latest
[ ] @next
[ x] 0.2.25 (or put your version here)
Steps to reproduce or a small repository showing the problem:
When I try to get all my posts whit the relation "comments" I get the data in disorder. I don't know if that is normal or how to fix it.
For example, If I make a SELECT * FROM posts;
I get
123, 124, 125, 126, 127
But TypeOrm gets
127, 123, 125, 126, 126 (Always in the same order)
I get the data in this way
const result = await postRepository.find({relations: ["user", "comments"]});
These are my post, user and comment entities. Post:
import {PrimaryGeneratedColumn, Column, Entity, ManyToOne, OneToMany} from "typeorm";
import {User} from "./User";
import {Comment} from "./Comment";
import {Like} from "./Like";
@Entity()
export class Post {
@PrimaryGeneratedColumn("uuid")
id: string;
@Column()
body: string;
@Column({default: () => "CURRENT_TIMESTAMP"})
created_at: Date;
@ManyToOne(type => User, user => user.posts, {onDelete: "CASCADE"})
user: User;
@OneToMany(type => Comment, comment => comment.post)
comments: Comment[];
@OneToMany(type => Like, like => like.post)
likes: Like[];
}
User:
import {Entity, PrimaryGeneratedColumn, Column, BeforeInsert, OneToMany, ManyToMany, JoinTable, Unique} from "typeorm";
import * as bcrypt from 'bcrypt';
import {Post} from "./Post";
import {Comment} from "./Comment";
import config from '../config/config';
import {Like} from "./Like";
@Unique(["email"])
@Unique(["username"])
@Entity()
export class User {
@PrimaryGeneratedColumn("uuid")
id: string;
@Column()
name: string;
@Column({name: "username"})
username: string;
@Column({name: "email"})
email: string;
@Column()
password: string;
@Column()
activated: boolean;
@Column({default: () => "CURRENT_TIMESTAMP"})
register_at: Date;
@OneToMany(type => Post, post => post.user)
posts: Post[];
@OneToMany(type => Comment, comment => comment.user)
comments: Comment[];
@OneToMany(type => Like, like => like.user)
likes: Like[];
@ManyToMany(type => User)
@JoinTable()
followers: User[];
public checkUnencryptedPassword(unencryptedPassword: string) {
return bcrypt.compareSync(unencryptedPassword, this.password);
}
@BeforeInsert()
encryptPassword() {
try {
this.password = bcrypt.hashSync(this.password, config.saltRounds);
} catch (e) {
console.log("Error encrypting");
}
}
}
Comment:
import {Entity, Column, ManyToOne, PrimaryGeneratedColumn, OneToMany} from "typeorm";
import {Post} from "./Post";
import {User} from "./User";
import {Like} from "./Like";
@Entity()
export class Comment {
@PrimaryGeneratedColumn("uuid")
id: string;
@Column()
body: string;
@Column({default: () => "CURRENT_TIMESTAMP"})
created_at: Date;
@ManyToOne(type => Post, post => post.comments, {onDelete: "CASCADE"})
post: Post;
@ManyToOne(type => User, user => user.comments, {onDelete: "CASCADE"})
user: User;
@OneToMany(type => Like, like => like.comment)
likes: Like[];
}
If this is not a bug why happens an how can I solve it? I need the data ordered.
Hmm, Now I see this is the normal behavior. How can I sort the array of posts by date inside the find() function?
Something like this should work. This will do a SQL ORDER BY
const result = await postRepository.find({relations: ["user", "comments"], order: {created_at: "ASC"}});
Thank you, sorry If I am to noob and my english isn't good :3