1 year ago

#384657

test-img

sunny

There is already an object named 'company_names' in the database

I use Gorm for Golang like this code,

func Connect() (*gorm.DB, error) {
var (
    err error
    db  *gorm.DB
)

dsn := "sqlserver://User:12345@127.0.0.1:1433?database=gorm"
db, err = gorm.Open(sqlserver.Open(dsn), &gorm.Config{})
if err != nil {
    return nil, err
}

err = db.Debug().AutoMigrate(&models.CompanyName{}, &models.CarModel{}, &models.CreateYear{}, &models.Diversity{})
if err != nil {
    return nil, err
}

return db, nil
}

and in main.go

func main() {
db, err := Connect()
if err != nil {
    panic("database connection failed")
}

ech := echo.New()
}

Ok in the first time when we run code, gorm create tables in database,but in the second time I got an error thet

mssql: There is already an object named 'table_names' in the database.

Do I have to delete db.Debug().AutoMigrate)(?

go

go-gorm

golang-migrate

0 Answers

Your Answer

Accepted video resources