Refering this help doc, there is a drop table if exists syntax db.DropTableIfExists(&User{}, "products")
but for db.Model(&User{}).DropColumn("description")
DropColumnIfExists doesn't exists. What should I use to implement DropColumn (if the column exists, otherwise not.)
DropColumn(如果存在于GORM中)
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
1条回答 默认 最新
- dongshi9526 2017-06-15 12:13关注
What should I use to implement DropColumn (if the column exists, otherwise not.)
To answer your question...
Go ahead with that. You can use
db.Model(&User{}).DropColumn("description")
.Just handle the errors gracefully. Remember, in Golang, Errors are values.
func main() { db.AutoMigrate(&User{}) err := db.Model(&User{}).DropColumn("description").Error if err != nil { // Do whatever you want to do! log.Print("ERROR: We expect the description column to be drop-able") } }
Under the Hood, gorm will execute raw postgresql query if it has no error. Otherwise, it will return the error.
I hope this will answer your question. :)
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥15 怎样才能让鼠标沿着线条的中心线轨迹移动
- ¥60 用visual studio编写程序,利用间接平差求解水准网
- ¥15 Llama如何调用shell或者Python
- ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
- ¥15 win10权限管理,限制普通用户使用删除功能
- ¥15 minnio内存占用过大,内存没被回收(Windows环境)
- ¥65 抖音咸鱼付款链接转码支付宝
- ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
- ¥15 blast算法(相关搜索:数据库)
- ¥15 请问有人会紧聚焦相关的matlab知识嘛?