Search code examples
gogo-xorm

xorm , difference between Sync and Sync2 functions


The xorm go library has 2 function that appear similar:

// Sync the new struct changes to database, this method will automatically add
// table, column, index, unique. but will not delete or change anything.
// If you change some field, you should change the database manually.
func (engine *Engine) Sync(beans ...interface{}) error 

// Sync2 synchronize structs to database tables
func (engine *Engine) Sync2(beans ...interface{}) error 

(While the Sync2() "docs" doesn't contain the explanation that it will add columns/indexes etc, the implementation does seem to do that too)

What is the difference between these two functions, and when should you use Sync and when should you use Sync2 ?


Solution

  • Have a look at https://gitea.com/xorm/xorm/src/commit/5fafa00043917e1253ebac95b5de29e2f08a569f/engine.go#L1115-L1128
    (which is the HEAD of the current master branch at the time of writing).

    As you can see there, apparently the developers noticed that as well, and deprecated Sync2 in favor of Sync. So, in short, you simply shouldn't use Sync2 at all anymore.