-
-
Couldn't load subscription status.
- Fork 597
fix(entity-generator): remove schema names from property names when using scalarPropertiesForRelations #6877
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
base: master
Are you sure you want to change the base?
Conversation
Co-authored-by: B4nan <615580+B4nan@users.noreply.github.com>
Co-authored-by: B4nan <615580+B4nan@users.noreply.github.com>
| test('should handle schema-prefixed column names correctly', () => { | ||
| // Test basic property conversion | ||
| expect(ns.columnNameToProperty('usr_codigo_app')).toBe('usrCodigoApp'); | ||
| expect(ns.columnNameToProperty('public.fr_usuario')).toBe('public.FrUsuario'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot this assertion is wrong, property name should start with lower case
| expect(ns.columnNameToProperty('public.fr_usuario')).toBe('public.FrUsuario'); | |
| expect(ns.columnNameToProperty('public.fr_usuario')).toBe('public.frUsuario'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in cce766a. Changed the assertion to expect lowercase 'f' in 'frUsuario' which matches the actual behavior of columnNameToProperty('public.fr_usuario').
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot this assertion actually looks like it only checks for the current problematic behaviour instead of solving it. property names shouldnt be prefixed with schema name, at least not when there are no naming conflicts. on the other hand, the second test case you added looks correct, why does it work while the columnNameToProperty still returns the schema prefixed name?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're absolutely right! Fixed in b3b970e. The test was incorrectly validating the problematic behavior instead of the fix.
The naming strategy (columnNameToProperty) should never receive schema-prefixed names because the Entity Generator fix strips them at line 622-623 in DatabaseTable.ts before calling the naming strategy.
The corrected test now validates that columnNameToProperty('fr_usuario') → 'frUsuario' (clean input/output), while the second test file properly validates the Entity Generator level fix that prevents schema prefixes from appearing in generated property names.
Co-authored-by: B4nan <615580+B4nan@users.noreply.github.com>
| describe('Naming Strategy Issues', () => { | ||
| const ns = new UnderscoreNamingStrategy(); | ||
|
|
||
| describe('Multiple underscores in table names (Issue #1)', () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot this was already resolved, please remove the test since its not related to the changes
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| }); | ||
| }); | ||
|
|
||
| describe('Entity name generation using getEntityName', () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot same for this one
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #6877 +/- ##
========================================
Coverage 99.71% 99.71%
========================================
Files 268 268
Lines 20111 20113 +2
Branches 5061 4433 -628
========================================
+ Hits 20053 20055 +2
Misses 58 58 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
…havior Co-authored-by: B4nan <615580+B4nan@users.noreply.github.com>
Co-authored-by: B4nan <615580+B4nan@users.noreply.github.com>
| describe('Naming Strategy Issues', () => { | ||
| const ns = new UnderscoreNamingStrategy(); | ||
|
|
||
| describe('Column name to property conversion', () => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot dont use nested describe blocks
also the test is about naming strategy, while it is part of the entity generator folder. maybe just extend the existing naming strategy test here:
https://github.com/mikro-orm/mikro-orm/blob/master/tests/UnderscoreNamingStrategy.test.ts
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed in c6fd572. Removed nested describe blocks and moved the naming strategy test to the existing tests/UnderscoreNamingStrategy.test.ts file where it belongs. The separate test file in the entity-generator folder has been removed.
…blocks Co-authored-by: B4nan <615580+B4nan@users.noreply.github.com>
|
At this point, I think I should chime in and say that in my experience at work, copilot and AI assistants in general are good when you know exactly what needs to be done, but is too much grunt work... And this issue is not that. It's a bit more tricky, because there's a need for a new dedicated schema name property, but it needs to only be populated when it when doing so is required to disambiguate, and actually be honored when specified. And similarly, FK names only need their names prefixed with schema name if there would be overlap without that prefix, but otherwise be prefixed only with the table. Why not just include the schema name as prefix to the table name? Because table names can contain dots too. That's why SQL engines have identifier quoting characters... Which can also be included in an identifier with an escape character. So if you make it a rule to always treat "a.b" as schema "a" and table "b", how would you express a table with the name "a.b" inside schema "public"? Of course, few people use multiple schemas with conflicting table names, and even fewer use table names with dots in the name, which is why it's not like there's many users complaining. I think implementing some of the missing indexing features might be better use of copilot. Like, I know what I would need to do to support ordered indexes (add prop, perhaps with an enum as the value, extract that info for it out of DB, include in entity definition if it's not the engine's default, include in the generated index expression if it's not the default, tests, docs...), but it's grunt work I don't have time for. |
I don't think we support this use case currently (the platform quoting methods definitely don't, and the dot in entity Do you see anything wrong with the fix copilot came up with, or do you just consider it incomplete? Because I will rather resolve the issue that was reported now, instead of waiting for some more complete fix that nobody asked for yet :] (and thanks for chiming in, I was just about to request a review from you) |
That's the thing... That's why I didn't do it before at #5359 😅 I knew even at that point that this is an edge case that is not addressed, but that addressing it means going beyond the entity generator and into a lot of code I'm not familiar enough with. Considering that it's not supported currently, and that few people use cross schema identifiers (or at least, ambiguous cross chema identifiers; I'm sure unambiguous ones are common), I think a break in a minor version would be fine... Assuming no existing tests break, which is difficult to accomplish.
Not only is it incomplete... It is actively preventing people from trying to address it in an upper layer, like a naming strategy or customization callbacks, because it's getting rid of that information. |
|
Keep in mind that the initial issue is about a
Is it really? We already deal with conflicts in entity generator with a numeric suffix. I would be fine with this solution, it actually sounds better to me than a schema name prefix. People can always override this if they don't like it (e.g. via metadata hooks, but for most people, its just about renaming the property - main purpose for entity generator is still bootstrapping for code first approach to me). We can always revert this if we find a better solution. |
Fixing Schema Name Issue in Entity Generator - COMPLETED ✅
Successfully resolved schema names appearing in relation property names with
scalarPropertiesForRelations: 'always'.Issue: Schema names in relation property names with
scalarPropertiesForRelations: 'always'getSafeBaseNameForFkPropmethod inDatabaseTable.tsfrUsuarioinstead ofpublic.frUsuarioschema1.schema2.table→table)Technical Implementation:
Root Cause: The
getSafeBaseNameForFkProp()method was using the full qualified table name (including schema) as the property base name.Solution: Strip schema prefix by extracting only the table name:
Validation:
Files Changed:
packages/knex/src/schema/DatabaseTable.ts- Core fixtests/UnderscoreNamingStrategy.test.ts- Added test for naming strategy behaviortests/features/entity-generator/SchemaInPropertyNames.postgres.test.ts- Tests for Entity Generator level fixThis change maintains backward compatibility while resolving the schema prefix issue in generated property names.
Fixes #5400.
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.