xorm,Sync和Sync2函数之间的区别是什么?

huangapple go评论116阅读模式
英文:

xorm , difference between Sync and Sync2 functions

问题

xorm go库有两个看起来相似的函数:

// 将新的结构更改同步到数据库,此方法将自动添加表、列、索引和唯一约束,但不会删除或更改任何内容。
// 如果更改了某些字段,应手动更改数据库。
func (engine *Engine) Sync(beans ...interface{}) error

// 将结构同步到数据库表
func (engine *Engine) Sync2(beans ...interface{}) error

(尽管Sync2()的“文档”中没有包含它将添加列/索引等的解释,但实现似乎也会执行这些操作)

这两个函数之间有什么区别?在什么情况下应该使用Sync,什么情况下应该使用Sync2?

英文:

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 ?

答案1

得分: 0

请看 https://gitea.com/xorm/xorm/src/commit/5fafa00043917e1253ebac95b5de29e2f08a569f/engine.go#L1115-L1128(这是我写作时当前“master”分支的“HEAD”)。

正如你在那里看到的,显然开发人员也注意到了这一点,并且已经弃用了Sync2,而推荐使用Sync。所以,简而言之,你根本不应该再使用Sync2了。

英文:

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.

huangapple
  • 本文由 发表于 2022年2月10日 04:47:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/71056501.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定