nestjsx / crud

NestJs CRUD for RESTful APIs
https://github.com/nestjsx/crud/wiki
MIT License
4.04k stars 533 forks source link

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

Open ghostlexly opened 2 years ago

ghostlexly commented 2 years ago

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.

ludovit-ubrezi commented 2 years ago

Did you found out how it can be used with limit? Because it is basic paging functionality ... trying to tag team member @fwoelffel

zetti-caletti commented 1 year ago

Have same problem. It appears if limit is set. I have made a PR: #808

Kannan18Msc commented 6 months ago

please try as it is entities field name @ghostlexly Screenshot 2024-03-06 at 2 34 00 PM Screenshot 2024-03-06 at 2 35 49 PM