Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Sep 23, 2025

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'

  • FIXED: Modified getSafeBaseNameForFkProp method in DatabaseTable.ts
  • ✅ Relations now generate clean property names like frUsuario instead of public.frUsuario
  • ✅ Schema prefixes are properly stripped when determining property base names
  • ✅ Handles complex schema hierarchies (e.g., schema1.schema2.tabletable)

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:

// Strip the schema name from the referenced table name for property naming
const parts = currentFk.referencedTableName.split('.');
return parts.length > 1 ? parts[parts.length - 1] : currentFk.referencedTableName;

Validation:

  • ✅ All TypeScript checks pass
  • ✅ All linting rules pass
  • ✅ Tests added to existing naming strategy test file
  • ✅ Edge cases handled (nested schemas, simple tables)
  • ✅ No regressions detected
  • ✅ Manually validated exact issue scenarios

Files Changed:

  • packages/knex/src/schema/DatabaseTable.ts - Core fix
  • tests/UnderscoreNamingStrategy.test.ts - Added test for naming strategy behavior
  • tests/features/entity-generator/SchemaInPropertyNames.postgres.test.ts - Tests for Entity Generator level fix

This 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.

Copilot AI and others added 2 commits September 23, 2025 22:15
Co-authored-by: B4nan <615580+B4nan@users.noreply.github.com>
Co-authored-by: B4nan <615580+B4nan@users.noreply.github.com>
Copilot AI changed the title [WIP] Naming errors in Entity Generator fix(entity-generator): remove schema names from property names when using scalarPropertiesForRelations Sep 23, 2025
Copilot AI requested a review from B4nan September 23, 2025 22:18
Copilot finished work on behalf of B4nan September 23, 2025 22:18
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');
Copy link
Member

@B4nan B4nan Sep 24, 2025

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

Suggested change
expect(ns.columnNameToProperty('public.fr_usuario')).toBe('public.FrUsuario');
expect(ns.columnNameToProperty('public.fr_usuario')).toBe('public.frUsuario');

Copy link
Contributor Author

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').

Copy link
Member

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?

Copy link
Contributor Author

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>
Copilot AI requested a review from B4nan September 24, 2025 07:45
Copilot finished work on behalf of B4nan September 24, 2025 07:45
describe('Naming Strategy Issues', () => {
const ns = new UnderscoreNamingStrategy();

describe('Multiple underscores in table names (Issue #1)', () => {
Copy link
Member

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed in 1059573. You're right - the multiple underscores tests were unrelated since Issue #1 was already fixed in PR #5359. The test file now only contains tests relevant to the actual fix I implemented.

});
});

describe('Entity name generation using getEntityName', () => {
Copy link
Member

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed in 1059573. The getEntityName tests were also unrelated to the schema prefix fix and have been removed along with the other Issue #1 tests.

@codecov
Copy link

codecov bot commented Sep 24, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.71%. Comparing base (5937ab6) to head (c6fd572).
⚠️ Report is 2 commits behind head on master.

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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

…havior

Co-authored-by: B4nan <615580+B4nan@users.noreply.github.com>
Copilot AI requested a review from B4nan September 24, 2025 08:05
Copilot finished work on behalf of B4nan September 24, 2025 08:05
Co-authored-by: B4nan <615580+B4nan@users.noreply.github.com>
Copilot finished work on behalf of B4nan September 24, 2025 08:13
Comment on lines 3 to 6
describe('Naming Strategy Issues', () => {
const ns = new UnderscoreNamingStrategy();

describe('Column name to property conversion', () => {
Copy link
Member

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

Copy link
Contributor Author

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>
Copilot AI requested a review from B4nan September 24, 2025 08:48
Copilot finished work on behalf of B4nan September 24, 2025 08:48
@boenrobot
Copy link
Collaborator

boenrobot commented Sep 24, 2025

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.

@B4nan
Copy link
Member

B4nan commented Sep 24, 2025

Because table names can contain dots too.

I don't think we support this use case currently (the platform quoting methods definitely don't, and the dot in entity tableName is used as a schema/table separator currenlty). And I feel like the refactorings to support this would be quite significant (and of course, likely breaking).

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)

@boenrobot
Copy link
Collaborator

I don't think we support this use case currently (the platform quoting methods definitely don't, and the dot in entity tableName is used as a schema/table separator currenlty). And I feel like the refactorings to support this would be quite significant (and of course, likely breaking).

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.

Do you see anything wrong with the fix copilot came up with, or do you just consider it incomplete?

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.

@B4nan
Copy link
Member

B4nan commented Sep 24, 2025

Keep in mind that the initial issue is about a public prefix, that should literally never happen, default schema should be omitted.

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.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Naming errors in Entity Generator

3 participants