gormigrate icon indicating copy to clipboard operation
gormigrate copied to clipboard

How does people table name refer to Person struct in the example/tests?

Open vorotech opened this issue 3 years ago • 2 comments

👋 I'm trying to understand how the people table name linked to Person{} as specified in README and tests.

Initially I was trying to set a custom name, and in gorm we usually has the func similar to following to specify the table name:

func (Person) TableName() string {
	return "people"
}

But since the suggestions is to freeze the struct with the migration by specifying inside the function, it's not clear how the following code really works or has a typo:

		// create persons table
		{
			ID: "201608301400",
			Migrate: func(tx *gorm.DB) error {
				// it's a good pratice to copy the struct inside the function,
				// so side effects are prevented if the original struct changes during the time
				type Person struct {
					gorm.Model
					Name string
				}
				return tx.AutoMigrate(&Person{})
			},
			Rollback: func(tx *gorm.DB) error {
				return tx.Migrator().DropTable("people")
			},
		},

vorotech avatar Aug 23 '21 14:08 vorotech

@andreynering

the README example has wrong information as mentioned above not working for me, maybe others can share their example code that works for initializing database or applying migrations because the current README is misleading

encryptblockr avatar Jan 23 '22 01:01 encryptblockr

Error in this example. GORM function DropColumn need to pass a structure for this function. Use it like this:

Rollback: func(tx *gorm.DB) error {
				type Person struct {
				}
				return tx.Migrator().DropColumn(&Person{}, "age")
			},

pavlozt avatar Feb 02 '22 19:02 pavlozt

Hi @vorotech, thank you for your contribution. You are right, the usage example in the README.md was outdated/incorrect and now we managed to rework it in https://github.com/go-gormigrate/gormigrate/pull/198. Please, check it out and let us know if there is anything that still looks confusing to you.

@encryptblockr Please check the usage example from README once again and let us know if there is anything still looks misleading or could be improved.

@pavlozt Thank you for your help.

avakarev avatar May 31 '23 01:05 avakarev