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

fix(database): add prefix to avoid join column name conflicts #20027

Merged
merged 17 commits into from Apr 17, 2024

Conversation

innerdvations
Copy link
Contributor

@innerdvations innerdvations commented Apr 4, 2024

What does it do?

Uses an alias for the join column name to prevent it conflicting with a column name on the main table.

Why is it needed?

If the join column name was the same as a column name in the main content type's table, it was always overwritten.

So for example, in #18085 , if we create a shirt content type with a shirt_id attribute, and then have a relation to it, the join was using shirt_id as the join column name, overwriting the shirt_id attribute of shirt.

Technically this is still the case, but it's extremely unlikely someone would have an attribute in a content type named __strapi{ct name}_id unless they were intentionally trying to encounter this issue. Regardless, In Strapi 5 it will be an officially reserved prefix.

This should be safe for long identifiers in the db, because any existing database with a join table will include an index named
{ct name}_{attribute name}_unique
Which means, given a minimum 1 char for ct name, we have len(1__unique) = 9 > len(__strapi) and should not hit any limit that isn't already being hit.

How to test it?

see #18085

Related issue(s)/PR(s)

Fixes #18085

DX-1178

@innerdvations innerdvations self-assigned this Apr 4, 2024
@innerdvations innerdvations added source: core:database Source is core/database package pr: fix This PR is fixing a bug labels Apr 4, 2024
Copy link

vercel bot commented Apr 8, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
contributor-docs ✅ Ready (Inspect) Visit Preview 💬 Add feedback Apr 12, 2024 1:43pm

@innerdvations innerdvations marked this pull request as ready for review April 8, 2024 14:31
@innerdvations innerdvations added this to the 4.23.1 milestone Apr 9, 2024
@innerdvations
Copy link
Contributor Author

innerdvations commented Apr 10, 2024

@pwizla This is targeting v4, and could technically be considered a breaking change although it is very unlikely to affect anyone, but we should probably document somewhere that you can't use __strapi{content type name}_id as the name of an attribute just so it's not lost and forgotten.

Copy link

sonarcloud bot commented Apr 11, 2024

Quality Gate Passed Quality Gate passed

Issues
5 New issues
0 Accepted issues

Measures
0 Security Hotspots
No data about Coverage
2.6% Duplication on New Code

See analysis details on SonarCloud

@innerdvations
Copy link
Contributor Author

innerdvations commented Apr 12, 2024

This somehow breaks pagination in previews, investigating it now. Fixed, only had an incomplete fix in oneToMany joinTables

Copy link
Member

@christiancp100 christiancp100 left a comment

Choose a reason for hiding this comment

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

QAed this and works as expected!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
echoes/type: bug Effort to correct undesirable or incorrect behavior pr: fix This PR is fixing a bug source: core:database Source is core/database package
Projects
Status: To be reviewed (Open)
Development

Successfully merging this pull request may close these issues.

Adding properties to entities named "<entity name>Id" cause issues when nesting relationship fields
4 participants