I have already an Ent model in my go project. But I need to remove some columns from the model.
It not works when I update my model. Running migration results runtime error.
How can I remove a column from existing model in Ent. ORM?
Asked
Active
Viewed 550 times
1

S.M.Mousavi
- 5,013
- 7
- 44
- 59
1 Answers
1
You can change model simply to remove some fields and this will works if you set WithDropIndex
and WithDropColumn
options within migration as follow:
package main
import (
"context"
"log"
"<project>/ent"
"<project>/ent/migrate"
)
func main() {
client, err := ent.Open("mysql", "root:pass@tcp(localhost:3306)/test")
if err != nil {
log.Fatalf("failed connecting to mysql: %v", err)
}
defer client.Close()
ctx := context.Background()
// Run migration.
err = client.Schema.Create(
ctx,
migrate.WithDropIndex(true),
migrate.WithDropColumn(true),
)
if err != nil {
log.Fatalf("failed creating schema resources: %v", err)
}
}
For more information see documentation

S.M.Mousavi
- 5,013
- 7
- 44
- 59