Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I have a problem with the relationship. @ManyToOne #191

Closed
Fokir opened this issue Jan 11, 2017 · 4 comments
Closed

I have a problem with the relationship. @ManyToOne #191

Fokir opened this issue Jan 11, 2017 · 4 comments
Labels

Comments

@Fokir
Copy link

Fokir commented Jan 11, 2017

There are 2 models

@Table()
export class Token extends Model {
    static __repository: Repository<Token>;

    @PrimaryGeneratedColumn()
    id: number;

    @Column({unique: true})
    token: string;

    @ManyToOne(type => User, (user: User) => user.tokens)
    @JoinColumn()
     user: Promise<User>; 
}
@Table()
export class User extends Model{
    static __repository: Repository<User>;
    __hide: Object = {password: true, email: function (u: User, r: Request) {
        if(r.user && u.id == r.user.id) return false;
        return true;
    }};

    @PrimaryGeneratedColumn()
    id: number;

    @Column({nullable: true, unique: true})
    email: string;

    @Column({nullable: true})
    password: string;

    @Column({unique: true})
    uplay: string;

    @ManyToMany(type=>Role, (role:Role)=>role.users)
    roles: Promise<Array<Role>>;

    @OneToMany(type=>Token, (token: Token)=>token.user)
    tokens: Promise<Array<Token>>;
}

The database data is stored as it should, but when I try to authenticate the user on the token, a promise Returns the void
let token: Token = await Token.__repository.findOne({token: request.get<string>('token')}); ...
let user: User = await token.user; user == undefined => true

executing query: SELECT user.id AS user_id, user.email AS user_email, user.password AS user_password, user.uplay AS user_uplay FROM user user INNER JOIN token Token ON Token.user=user.id WHERE user.id=? -- PARAMETERS: [6]

This is carried out such a request.
It is not true. Instead the user id is inserted record id of the found token.

I can not understand where I was wrong

@Fokir Fokir changed the title I had a problem with the relationship. @ManyToOne I have a problem with the relationship. @ManyToOne Jan 11, 2017
@Fokir
Copy link
Author

Fokir commented Jan 11, 2017

image
image

When searching for a token identifier equal to 3, and further loading the user relationship, there is such a request

executing query: SELECT user.id AS user_id, user.email AS user_email, user.password AS user_password, user.uplay AS user_uplay FROM user user INNER JOIN token Token ON Token.user=user.id WHERE user.id=? -- PARAMETERS: [3]

@pleerock
Copy link
Member

that was a bug. fixed and released in 0.0.7-alpha.8. Do npm i typeorm@0.0.7-alpha.8 to try it.

@Fokir
Copy link
Author

Fokir commented Jan 11, 2017

Thank you very much, everything works as expected. So many hours has killed in order to understand what's the error

@pleerock
Copy link
Member

ahh sorry about that

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants