bugfix: set schema when clearing foreign key edges #4429
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Change Description
I am fixing a one-liner bug in
ent/dialect/sql/sqlgraph/graph.go
in which the schema was not being set when clearing foreign key relationships in the functionclearFKEdges
that results in always clearing the reverse edge in the public schema, even when the ent client was configured to use another schema.Context
I recently encountered an issue in which clearing a O2O bi-directional relationship cleared the field in for objects in two different schemas.
In our database, we generate copies of our public and data for manipulating data without affecting production. When clearing one side of a O2O, bidirectional relationship, I saw that the SQL commands were clearing the foreign keys for the relationship in the development schema for one object and the public schema for the other.
As an example, I have a table called
intfaces
that has a O2O, bidirectional relationship to itself represented by a foreign key from its columnid
to another columnpeer_id
. When clearing the peer_id in one object in my development schema, it cleared the edge in the development schema but the reverse edge in the public schema, resulting in the following SQL commands being generated:Solution
Adding the
Schema
function to the*sql.UpdateBuilder
inclearFKEdges
fixes this issue. Here is the above operation's resulting SQL commands after the fix is implemented:I have created a local patch of the
ent/ent
repo in our project repository with the oneliner, bugfix that we will use until this PR is merged and the bugfix is released.For additonal context, the
addFKEdges
already sets the Schema in its*sql.UpdateBuilder
(here). This fix bringsclearFKEdges
inline withaddFKEdges
in its support for multiple schemas.