[relationJoins] join_alias_name
exceeding maximum postgres identifier length -> results in table name "..." specified more than once
error
#26117
Labels
bug/1-unconfirmed
Bug should have enough information for reproduction, but confirmation has not happened yet.
domain/client
Issue in the "Client" domain: Prisma Client, Prisma Studio etc.
kind/bug
A reported bug.
tech/engines
Issue for tech Engines.
topic: postgresql
topic: relationJoins
Description
Lateral joins generated by
LateralJoinSelectBuilder
are aliased by a concatenation of model name and field name generated injoin_alias_name
. https://github.com/prisma/prisma-engines/blob/5e70d191c94f7d16f2ebf48387832b89f72d2ec3/query-engine/connectors/sql-query-connector/src/query_builder/select/mod.rs#L660-L662This works fine as long as the length of the model name, field name and underscore together do not exceed 63 characters.
While Postgres allows including longer identifiers in SQL commands, it will truncate / discard any parts of the identifier that exceed 63 characters.
https://www.postgresql.org/docs/current/sql-syntax-lexical.html#SQL-SYNTAX-IDENTIFIERS
Knowing the limitations of PostgreSQL, Prisma users would expect 63 characters as an upper limit for unambiguous identifiers for both the model as well as the field. By joining the two together, this can still result in an alias with up to 127 characters, that Postgres can no longer handle unambigously.
This has lead to Postgres Query errors
table name "..." specified more than once
on our project.Contrived example
(Not related to our project, just to illustrate the issue)
join_alias_name
will turn these into:Postgres will truncate both to the same value
ExtendedPassengerAutomobileWarrantyMaintainanceEvent_special_in
, complaining abouttable name "ExtendedPassengerAutomobileWarrantyMaintainanceEvent_special_in" specified more than once
if both of them appear in the same query.Possible solution
A possible solution for this issue may be to drop the
join_alias_name
andm2m_join_alias_name
functions and replace them with aliases generated bynext_alias
instead. I tried drafting a PR for this, but unfortunately don't have enough insight yet, how the different code locations using this function (namelybuild_selection
,add_to_one_relation
andbuild_json_obj_fn
) fit together.Would appreciate some pointers to help me get started in the right direction.
CC @Weakky
The text was updated successfully, but these errors were encountered: