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

Incorrect table mapping when using EntityGenerator with entitySchema #3915

Closed
koneru9999 opened this issue Jan 6, 2023 · 3 comments · Fixed by #3916
Closed

Incorrect table mapping when using EntityGenerator with entitySchema #3915

koneru9999 opened this issue Jan 6, 2023 · 3 comments · Fixed by #3916
Labels
bug Something isn't working

Comments

@koneru9999
Copy link
Contributor

Describe the bug
When using the EntityGenerator helper to generate the entities from existing database schema, the generated entity is mapped to wrong or non-existing table name especially when the table name has underscore (_).

This may be happening for other special characters as well.

Stack trace

...

To Reproduce
Steps to reproduce the behavior:

  1. Create a table Address_lines
CREATE TABLE `Address_lines` (
`nodeId` char(25) CHARACTER SET utf8 NOT NULL,
`position` int(4) NOT NULL,
`value` mediumtext COLLATE utf8mb4_unicode_ci NOT NULL,
PRIMARY KEY (`nodeId`,`position`),
KEY `value` (`value`(191))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
  1. setup the entity generation
const orm = await init({
  // ... other config
  entitiesTs: [`./src/entities`],
  entityGenerator: {
    entitySchema: true,
    identifiedReferences: true,
  },
  type: 'mysql',
})

await orm.getEntityGenerator().generate({
  baseDir: path.join(__dirname, `./src/entities`),
  save: true,
})

await orm.close(true)
  1. the following is the generated after the execusting the generation
  export class AddressLines {
    nodeId!: string
    position!: number
    value!: unknown
  }

export const AddressLinesSchema = new EntitySchema({
  class: AddressLines,
  tableName: 'AddressLines', // This is wrong and should be inferred as `Address_lines`
  properties: {
    nodeId: {
      primary: true,
      fieldName: 'nodeId',
      columnType: 'char(25)',
      length: 25 
    },
    position: { primary: true, type: 'number' },
    value: {
      type: 'unknown',
      columnType: 'mediumtext',
      length: 16777215,
      index: 'value',
    },
  },
})

Expected behavior
Entity class and schema files generated with corresponding table name.

tableName: 'Address_lines',

Additional context
A possible issue could be in this line https://github.com/mikro-orm/mikro-orm/blob/master/packages/entity-generator/src/EntitySchemaSourceFile.ts#L45

Versions

Dependency Version
node 16.x
typescript 4.9.4
mikro-orm 5.6.3
your-driver mysql
@B4nan B4nan added the bug Something isn't working label Jan 8, 2023
B4nan added a commit that referenced this issue Jan 8, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
…ySchema` (#3916)

Closes #3915

Co-authored-by: Martin Adámek <banan23@gmail.com>
@koneru9999
Copy link
Contributor Author

Hi @B4nan,
Thanks for following up on the PR.

Any plan on getting this released this week ?

@B4nan
Copy link
Member

B4nan commented Jan 9, 2023

every commit to master is released as dev version, so you can use it right now. will do a stable release later this week, maybe even today

@koneru9999
Copy link
Contributor Author

Thanks. good to know 👍

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

Successfully merging a pull request may close this issue.

2 participants