Skip to content

One-To-Many / Many-To-One cannot be query/joined #779

@ghostlexly

Description

@ghostlexly

Hello, i'm new on nestjs,
i have an issue with the join of these two entities, can't find why...
any help ?..

Controller

@Crud({
  model: {
    type: Housekeeper,
  },
  query: {
    join: {
      geozones: {
        alias: "test",
        allow: ["postalCode"],
        eager: true,
      },
    },
  },
})
@ApiTags("housekeepers")
@Controller("housekeepers")
export class HousekeepersController implements CrudController<Housekeeper> {

Geozone.ts

@Entity()
export class Geozone extends BaseEntity {
  @PrimaryGeneratedColumn()
  id: number;

  @IsDefined()
  @Validate(RelationExists, [{ targetEntity: "housekeeper", targetField: "id" }])
  @ApiProperty({ type: "number" })
  @ManyToOne(() => Housekeeper, (hs) => hs.geozones)
  @JoinColumn()
  owner: Housekeeper;

  @ApiProperty()
  @Column()
  postalCode: string;
}

Housekeeper.ts

@Entity()
export class Housekeeper extends BaseEntity {
  @PrimaryGeneratedColumn()
  id: number;

  @ApiProperty()
  @IsEmail()
  @Column({ unique: true })
  email: string;

  @OneToMany(() => Geozone, (geozone) => geozone.owner)
  geozones: Geozone[];
}

Here is the error:

backend_1       | [Nest] 2197  - 04/16/2022, 6:27:43 PM     LOG [NestApplication] Nest application successfully started +1ms
backend_1       | [Nest] 2197  - 04/16/2022, 6:28:24 PM   ERROR [ExceptionsHandler] Duplicate column name 'Housekeeper_id'
backend_1       | QueryFailedError: Duplicate column name 'Housekeeper_id'
backend_1       |     at Query.onResult (/usr/src/app/src/driver/mysql/MysqlQueryRunner.ts:222:33)
backend_1       |     at Query.execute (/usr/src/app/node_modules/mysql2/lib/commands/command.js:36:14)
backend_1       |     at PoolConnection.handlePacket (/usr/src/app/node_modules/mysql2/lib/connection.js:456:32)
backend_1       |     at PacketParser.onPacket (/usr/src/app/node_modules/mysql2/lib/connection.js:85:12)
backend_1       |     at PacketParser.executeStart (/usr/src/app/node_modules/mysql2/lib/packet_parser.js:75:16)
backend_1       |     at Socket.<anonymous> (/usr/src/app/node_modules/mysql2/lib/connection.js:92:25)
backend_1       |     at Socket.emit (node:events:390:28)
backend_1       |     at addChunk (node:internal/streams/readable:315:12)
backend_1       |     at readableAddChunk (node:internal/streams/readable:289:9)
backend_1       |     at Socket.Readable.push (node:internal/streams/readable:228:10)

update:
i found that this issue appears only when "limit" or "maxLimit" is set in query.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions