Golang GORM中的列自动迁移问题

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

Auto-migration issues with columns in Golang GORM

问题

自动迁移存在问题(我认为)。我可以通过psql控制台确认该列不存在。我可以通过终端/控制台/SQL手动插入该列,但更希望通过自动迁移解决此问题。

谢谢您的阅读和/或行动。

终端输出:

  • 启动Web服务器:"(pq: column "password_hash" contains null values)"
  • 提交POST请求:"(pq: column "password_hash" of relation "accounts" does not exist)"
英文:

Issues with auto migration (i think). I can conclude the column does not exist via psql console. I can insert the column manually via terminal/console/SQL but prefer to resolve this with auto migration.

Thank you for your read and/or action.

TERMINAL OUTPUT:

  • Start Web Server: "(pq: column "password_hash" contains null
    values)
    "
  • Submit POST: "(pq: column "password_hash" of relation "accounts" does not exist)"

答案1

得分: 2

来源:https://www.usegolang.com/

“...简而言之,automigrate无法运行,因为当它添加列并声明不能有空值时,这与任何现有记录默认具有空值的事实相矛盾。最简单的解决方法是,由于您仍处于开发环境中,可以通过删除表中的所有用户来解决,可以通过删除整个表或运行一些SQL来实现。”

非常感谢Jon Calhoun提供的这个简单而有效的解决方案。

英文:

Credit: Jon Calhoun

Resource: https://www.usegolang.com/

>"...the short answer is that automigrate fails to run because when it adds the column and says it can't have null values this contradicts the fact that any existing records will have a null value by default. The simplest fix since you are still in a dev env is to delete all the users from your table either by dripping the entire table or by running some SQL."

Many thanks to Jon Calhoun for this simple and effective solution.

huangapple
  • 本文由 发表于 2017年5月26日 03:20:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/44188272.html
匿名

发表评论

匿名网友

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

确定