typeorm: ManyToOne eager query error

Issue type:

[ ] question [x] bug report [ ] feature request [ ] documentation issue

Database system/driver:

[ ] cordova [ ] mongodb [ ] mssql [ ] mysql / mariadb [ ] oracle [x] postgres [ ] sqlite [ ] sqljs [ ] react-native [ ] expo

TypeORM version:

[ ] latest [x] @next [ ] 0.x.x (or put your version here)

Steps to reproduce or a small repository showing the problem:

I have the following entities:

PostComment

@Entity()
export class PostComment  {

  @PrimaryGeneratedColumn()
  id: number;

  @Column({ default: true })
  active: boolean;

  @Column({
    type: String,
    nullable: true,
  })
  authorName?: String;

  @Column({
      type: String,
      nullable: true,
  })
  comment?: String;

  @ManyToOne(_ => Post, post => post.comments, {eager: true})
  @JoinColumn()
  post: Post;

}

Post

@Entity()
export class Post {

  @PrimaryGeneratedColumn()
  id: number;

  @Column({ default: true })
  active: boolean;

  @Column()
  title: string;

  @Column()
  text: string;

  @OneToMany(_ => PostComment, comment => comment.post, {
    cascade: true,
  })
  comments: PostComment[];

}

When try to find all PostComment with an post id: repository.find({ post: { id: 1 } })

it gives the following error: table name "PostComment_post" specified more than once.

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 6
  • Comments: 17 (6 by maintainers)

Commits related to this issue

Most upvoted comments

Still experiencing this bug, as described above, with TypeORM version 0.2.18.

Can the issue be re-opened for examination?

@sgarner I’ve solved similar bug with custom naming strategy

class NamingStrategy extends DefaultNamingStrategy {
    eagerJoinRelationAlias(alias: string, propertyPath: string): string {
        let out = alias + '_' + propertyPath.replace('.', '_')
        let match = out.match(/_/g) || []
        return out + match.length
    }
}
let connectionOptions = await getConnectionOptions()
await createConnection(
        Object.assign(connectionOptions, {
            namingStrategy: new NamingStrategy(),
        })
    )

I confirm the error in version 0.2.16

Some observations:

  1. This throw ER_DUP_FIELDNAME exception
@ManyToOne(type => Currency, {
        eager: true,
})
@JoinColumn({name: 'currency_id'})
  1. All of this variants work perfectly:

2.1 Without eager

@ManyToOne(type => Currency)
@JoinColumn({name: 'currency_id'})
currency: Currency

2.1 Without custom field name. In this case in table name is currencyId

@ManyToOne(type => Currency, {
        eager: true,
})
@JoinColumn()
currency: Currency

2.2 With field name other than currency_id in my case

@ManyToOne(type => Currency, {
        eager: true,
})
@JoinColumn({name: 'other_name_id'})
currency: Currency

After some digging, I realized this error is due to Typeorm creating some kind of variable when using eager loading that is longer than Postgres limit for names.

For example, if you are eager loading products with customer, typeorm will create something along the lines of customer_products, connecting the two. If that name is longer than 63 bytes (Postgres limit) the query will crash.

I’m not 100% sure about how everything works under the hood but this is the summary of it I think.

Also experiencing this, 0.2.25

Experiencing the same issue on @OneToMany

Version: 0.2.25

Also experiencing this, 0.2.25

On @next version, I confirm this issue eager loading does not work on @ ManyToOne relation.

It causes duplicate errors like these : ER_NONUNIQ_TABLE: Not unique table/alias ER_DUP_FIELDNAME: Duplicate column name

I’ve encountered same errors. It was said to be fixed in 0.2.14, but my TypeORM is in 0.2.16.

(node:4119) UnhandledPromiseRejectionWarning: QueryFailedError: ER_DUP_FIELDNAME: Duplicate column name 'User_faculty_id'
    at new QueryFailedError (/Users/hohoibin/Developments/graduate/node/src/error/QueryFailedError.ts:9:9)
    at Query.<anonymous> (/Users/hohoibin/Developments/graduate/node/src/driver/mysql/MysqlQueryRunner.ts:164:37)
    at Query.<anonymous> (/Users/hohoibin/Developments/graduate/node/node_modules/mysql/lib/Connection.js:502:10)
    at Query._callback (/Users/hohoibin/Developments/graduate/node/node_modules/mysql/lib/Connection.js:468:16)
    at Query.Sequence.end (/Users/hohoibin/Developments/graduate/node/node_modules/mysql/lib/protocol/sequences/Sequence.js:83:24)
    at Query.ErrorPacket (/Users/hohoibin/Developments/graduate/node/node_modules/mysql/lib/protocol/sequences/Query.js:90:8)
    at Protocol._parsePacket (/Users/hohoibin/Developments/graduate/node/node_modules/mysql/lib/protocol/Protocol.js:278:23)
    at Parser.write (/Users/hohoibin/Developments/graduate/node/node_modules/mysql/lib/protocol/Parser.js:76:12)
    at Protocol.write (/Users/hohoibin/Developments/graduate/node/node_modules/mysql/lib/protocol/Protocol.js:38:16)
    at Socket.<anonymous> (/Users/hohoibin/Developments/graduate/node/node_modules/mysql/lib/Connection.js:91:28)
(node:4119) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:4119) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.