casbin / gorm-adapter

GORM adapter for Casbin, see extended version of GORM Adapter Ex at: https://github.com/casbin/gorm-adapter-ex
https://github.com/casbin/casbin
Apache License 2.0
678 stars 206 forks source link

id increase when savePolicy #223

Closed wuzhican closed 11 months ago

wuzhican commented 11 months ago

I use PostgreSQL as the database, and when I SavePolicy, the adapter uses truncate to empty the policy table and then insert records. However, in practice, truncate does not reset the id count value after emptying the table, which causes the id to increment itself whenever the policy is saved as the program runs, and eventually overflows. Haven't had a problem with this issue yet, but OCD asked me if it's necessary to fix the problem with this place.

casbin-bot commented 11 months ago

@tangyang9464 @JalinWang @imp2002

hsluoyz commented 11 months ago

@wuzhican can you make a PR to fix it?

wuzhican commented 11 months ago

According to my test, adding Restart Identity after the truncate table in PostgreSQL can reset the sequence. However, other databases may not support the Restart Identity syntax and the Restart Identity does not take effect for the sequences in the table created manually by the user. More tests are needed before repairing. Another way to fix is to modify truncate to delete, which will reset the sequence, but this will be slower than truncate. Which repair method are you more inclined to?

hsluoyz commented 11 months ago

@wuzhican delete is good

wuzhican commented 11 months ago

please check my PR

wuzhican commented 11 months ago

After my retest, truncate resets the id in mysql and mssql. Postgres also resets the ID when it adds the restart identity. However, delete does not reset the ID for all three databases. As for the other databases, I did not test them.