Gorm: Automigrate: Change column size

Created on 21 Oct 2020  路  7Comments  路  Source: go-gorm/gorm

Your Question

Hi everyone,

I'm actually using the v2 of gorm. In the documentation, I found the following information

_NOTE: AutoMigrate will create tables, missing foreign keys, constraints, columns and indexes, and will change existing column鈥檚 type if it鈥檚 size, precision, nullable changed, it WON鈥橳 delete unused columns to protect your data._

https://gorm.io/docs/migration.html

I tried to change the size of one column using AutoMigrate but it appears that it is not working.
First of all, before creating any BUG issue, I wanted to double check that it is doable ?

Here is my code:

type Module struct {
    gorm.Model
    Name  string `gorm:"size:36"`
}

I only changed the size from 128 to 36
But nothing changed on my modules table.

image

The driver is a mysql one.

Thanks in advance @jinzhu 馃憤

question

Most helpful comment

@maeglindeveloper can confirm that it's related, the tests failing in this PR show that mysql does not have size information when calling ColumnTypes (used by AutoMigrate).

All 7 comments

EDIT: It seems that this issue is related as well
https://github.com/go-gorm/gorm/issues/3628
@jinzhu

@maeglindeveloper can confirm that it's related, the tests failing in this PR show that mysql does not have size information when calling ColumnTypes (used by AutoMigrate).

@maeglindeveloper can confirm that it's related, the tests failing in this PR show that mysql does not have size information when calling ColumnTypes (used by AutoMigrate).

Hi @asmeikal, I have tested postgres/mysql with datatime, seem them save the precision information into column datetime_precision, which doesn't work correctly right now, could you check it out?

I have updated tests to test this case https://github.com/go-gorm/gorm/tree/smart_auto_migrate

Hi @jinzhu, I'm having some problems with mysql. I've fixed the datetime precision issue, but this line fails with mysql.

The salary column is first created with data type double using this version of the entity.

Then the second version of the entity is not migrated correctly with mysql.

The desired type is decimal(2, 0) as returned by m.DB.Migrator().FullDataTypeOf(field).SQL, and the current data type is double as returned by columnType.DatabaseTypeName(). See the beginning of this function.

But the data type is not changed from double to decimal(2, 0), so the salary test fails. I'm not sure how to solve this.

I opened this PR for postgres and this PR for mysql for the datetime precision issue.

Hi @jinzhu, I'm having some problems with mysql. I've fixed the datetime precision issue, but this line fails with mysql.

The salary column is first created with data type double using this version of the entity.

Then the second version of the entity is not migrated correctly with mysql.

The desired type is decimal(2, 0) as returned by m.DB.Migrator().FullDataTypeOf(field).SQL, and the current data type is double as returned by columnType.DatabaseTypeName(). See the beginning of this function.

But the data type is not changed from double to decimal(2, 0), so the salary test fails. I'm not sure how to solve this.

I opened this PR for postgres and this PR for mysql for the datetime precision issue.

Just added a comment for the mysql driver.

Merged the PR, thank you for your awesome work ;)

Was this page helpful?
0 / 5 - 0 ratings