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

SQL error at populate ManyToMany relation. #234

Closed
ghost opened this issue Nov 11, 2019 · 3 comments
Closed

SQL error at populate ManyToMany relation. #234

ghost opened this issue Nov 11, 2019 · 3 comments
Assignees
Labels
bug Something isn't working

Comments

@ghost
Copy link

ghost commented Nov 11, 2019

Describe the bug
SQL error at populate ManyToMany relation using EntityRepository.find().

Stack trace

Error: (conn=52, no: 1054, SQLState: 42S22) Unknown column 'e0.$in' in 'where clause'
sql: select `e0`.*, `e1`.`artist_id`, `e1`.`style_id` from `artist` as `e0` left join `style_to_artist` as `e1` on `e0`.`id` = `e1`.`artist_id` where `e0`.`$in` in (?, ?, ?, ?, ?) and `e1`.`style_id` in (?, ?, ?, ?, ?, ?, ?, ?, ?, ?) group by `e0`.`id`, `e1`.`artist_id`, `e1`.`style_id` - parameters:[1,2,3,4,5,1,1,2,2,3,3,4,4,5,5]
    at Object.module.exports.createError (/home/alejandro/Documentos/Proyectos/tattoo/server/node_modules/mariadb/lib/misc/errors.js:55:10)
    at PacketNodeEncoded.readError (/home/alejandro/Documentos/Proyectos/tattoo/server/node_modules/mariadb/lib/io/packet.js:510:19)
    at Query.readResponsePacket (/home/alejandro/Documentos/Proyectos/tattoo/server/node_modules/mariadb/lib/cmd/resultset.js:46:28)
    at PacketInputStream.receivePacketBasic (/home/alejandro/Documentos/Proyectos/tattoo/server/node_modules/mariadb/lib/io/packet-input-stream.js:104:9)
    at PacketInputStream.onData (/home/alejandro/Documentos/Proyectos/tattoo/server/node_modules/mariadb/lib/io/packet-input-stream.js:160:20)
    at Socket.emit (events.js:210:5)
    at Socket.EventEmitter.emit (domain.js:476:20)
    at addChunk (_stream_readable.js:308:12)
    at readableAddChunk (_stream_readable.js:289:11)
    at Socket.Readable.push (_stream_readable.js:223:10)
    at TCP.onStreamRead (internal/stream_base_commons.js:182:23)
From previous event:
    at Client_MySQL._query (/home/alejandro/Documentos/Proyectos/tattoo/server/node_modules/knex/lib/dialects/mysql/index.js:124:12)
    at Client_MySQL.query (/home/alejandro/Documentos/Proyectos/tattoo/server/node_modules/knex/lib/client.js:158:17)
    at Runner.query (/home/alejandro/Documentos/Proyectos/tattoo/server/node_modules/knex/lib/runner.js:135:36)
    at /home/alejandro/Documentos/Proyectos/tattoo/server/node_modules/knex/lib/runner.js:39:23
From previous event:
    at Runner.run (/home/alejandro/Documentos/Proyectos/tattoo/server/node_modules/knex/lib/runner.js:25:16)
    at Builder.Target.then (/home/alejandro/Documentos/Proyectos/tattoo/server/node_modules/knex/lib/interface.js:14:43)

To Reproduce
Steps to reproduce the behavior:

entities/artistEntity.ts

import { Collection, Entity, IdentifiedReference, IdEntity, ManyToMany, OneToOne, PrimaryKey } from "mikro-orm";
import { AccountEntity } from "./accountEntity";
import { StyleEntity } from "./styleEntity";

@Entity({ collection: "artist" })
export class ArtistEntity implements IdEntity<ArtistEntity> {
    @PrimaryKey()
    public id!: number;

    @OneToOne(() => AccountEntity)
    public account!: IdentifiedReference<AccountEntity>;

    @ManyToMany(() => StyleEntity, (style) => style.artists)
    public styles: Collection<StyleEntity>;

    public constructor() {
        this.styles = new Collection(this);
    }
}

entities/styleEntity.ts

import { Collection, Entity, IdEntity, ManyToMany, PrimaryKey, Property } from "mikro-orm";
import { ArtistEntity } from "./artistEntity";

@Entity({ collection: "style" })
export class StyleEntity implements IdEntity<StyleEntity> {
    @PrimaryKey()
    public id!: number;

    @Property()
    public name!: string;

    @ManyToMany(() => ArtistEntity)
    public artists: Collection<ArtistEntity>;

    public constructor() {
        this.artists = new Collection(this);
    }
}

stores/styleStore.ts

import { EntityRepository } from "mikro-orm";
import { inject, singleton } from "tsyringe";
import { StyleEntity } from "../entities/styleEntity";
import OrmClient from "../ormClient";

@singleton()
export default class StyleStore {
    private styleRepository: EntityRepository<StyleEntity>;

    public constructor(@inject(OrmClient) ormClient: OrmClient) {
        this.styleRepository = ormClient.em.getRepository(StyleEntity);
    }

    public async artists(artists: number[]): Promise<Array<StyleEntity | undefined>> {
        const styles = await this.styleRepository.find({ artists }, ["artists"]);

        console.log(styles[0].artists);

        return [];
        // return artists.map((artist) => styles.find((style) => style.artists.id === artist));
    }
}

Expected behavior
No SQL error.

Additional context
I have an array of artists ids: [ 1, 2, 3, 4, 5 ] and I want all styles of these artists, using only a query and I need they on same order: [[styles of artist id: 1], [styles of artist id: 2], ...].

Versions

Dependency Version
node v12.13.0
typescript 3.6.3
mikro-orm 3.0.0-alpha.34
your-driver mariadb
@ghost ghost added the bug Something isn't working label Nov 11, 2019
@ghost ghost assigned B4nan Nov 11, 2019
@B4nan
Copy link
Member

B4nan commented Nov 11, 2019

Confirmed and replicated, thanks for the detailed report!

B4nan added a commit that referenced this issue Nov 11, 2019
@B4nan
Copy link
Member

B4nan commented Nov 11, 2019

Fixed in 3.0.0-alpha.36.

@B4nan B4nan closed this as completed Nov 11, 2019
@ghost
Copy link
Author

ghost commented Nov 12, 2019

Thanks to you for the fast resolution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant