Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

isazpiazu-roblox
Copy link

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 function clearFKEdges 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 column id to another column peer_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:

UPDATE "development"."intfaces" SET "peer_id" = NULL WHERE "id" = $1 args=[687169]
UPDATE "intfaces" SET "peer_id" = NULL WHERE "peer_id" = $1 args=[687169]

Solution

Adding the Schema function to the *sql.UpdateBuilder in clearFKEdges fixes this issue. Here is the above operation's resulting SQL commands after the fix is implemented:

UPDATE "development"."intfaces" SET "peer_id" = NULL WHERE "id" = $1 args=[687173]
UPDATE "development"."intfaces" SET "peer_id" = NULL WHERE "peer_id" = $1 args=[687173]

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 brings clearFKEdges inline with addFKEdges in its support for multiple schemas.

@isazpiazu-roblox isazpiazu-roblox changed the title Set schema when clearing foreign key edges bugfix: set schema when clearing foreign key edges Sep 11, 2025
@isazpiazu-roblox
Copy link
Author

Hi @a8m, would you be able to review this small bugfix. It's a one liner and has been fixed in our local patch to support a core piece of functionality in our project.

Please let me know if you have any questions, comments, or feedback about the change or PR. Thank you!

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.

1 participant