Ent. how to remove a column?

691 views Asked by At

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?

1

There are 1 answers

0
S.M.Mousavi On

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