Skip to content

Conversation

@OrkhanAlikhanov
Copy link
Contributor

@OrkhanAlikhanov OrkhanAlikhanov commented Jun 2, 2025

Original Rebuild was making thousands of individual update queries. I added RebuildBatched which can be
called as:

nestedset.RebuildBatched(db.WithContext(ctx), root, true, 1000)

The method is postgres specific. And can be used by adding following to go.mod file:

replace github.com/longbridgeapp/nested-set => github.com/OrkhanAlikhanov/nested-set v1.5.2

If the model has required fields, it will fail with ERROR: null value in column \"my_column" of relation \"my_table\" violates not-null constraint (SQLSTATE 23502). The workaround is to set default values or make columns nullable which is not great but may worth the change.

@huacnlee
Copy link
Member

huacnlee commented Jun 2, 2025

The method is postgres specific. And can be used by adding following to go.mod file:

Do you means this change only works for Postgres? If true, this is not acceptable, we need to keep support other databases.



// RebuildBatched rebuild nodes as any nestedset which in the scope
// ```nestedset.RebuildBatched(db, &node, true, 1000)``` will rebuild [&node] as nestedset
Copy link
Member

@huacnlee huacnlee Jun 2, 2025

Choose a reason for hiding this comment

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

Or, we need to add a doc here to describe the limitation.

@OrkhanAlikhanov
Copy link
Contributor Author

Thanks for the quick review.

Do you means this change only works for Postgres? If true, this is not acceptable, we need to keep support other databases.

Technically the library can't be used for mysql or sql server as it uses NULLS FIRST clause ( #22) but I made the PR in case someone wants similar behavior and can use it using go mod replace.

Also, another limitation is that if the table has non-null constraint with no default value, it fails with ERROR: null value in column \"my_column" of relation \"my_table\" violates not-null constraint (SQLSTATE 23502). The workaround is to set default values or make columns nullable which is not great.

@huacnlee
Copy link
Member

huacnlee commented Jun 2, 2025

Yes, I forget that.

@huacnlee huacnlee merged commit b2c6916 into longbridge:main Jun 2, 2025
1 check passed
@huacnlee
Copy link
Member

huacnlee commented Jun 2, 2025

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.

2 participants