Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change name of default generated columns #1480

Closed
sredxny opened this issue May 18, 2017 · 3 comments
Closed

Change name of default generated columns #1480

sredxny opened this issue May 18, 2017 · 3 comments

Comments

@sredxny
Copy link

sredxny commented May 18, 2017

Hi, is possible to override the name the columns: ID, created_at, updated_at, deleted_at ?

As far as I know I can set for example the name of the table with the function:


func (StructType) TableName() string {
	return "MyCustomName"
}

Is it possible but with the columns? this is because the data-model of the project already have stablished the namesof the fields, and for example we don't have updated_at but ModificationTimestamp so, how can I do it?

@smacker
Copy link
Contributor

smacker commented May 19, 2017

http://jinzhu.me/gorm/models.html#conventions

// Overriding Column Name
type Animal struct {
    AnimalId    int64     `gorm:"column:beast_id"`         // set column name to `beast_id`
    Birthday    time.Time `gorm:"column:day_of_the_beast"` // set column name to `day_of_the_beast`
    Age         int64     `gorm:"column:age_of_the_beast"` // set column name to `age_of_the_beast`
}

@sredxny
Copy link
Author

sredxny commented May 20, 2017

@smacker Hi, thank you. But I exactly am trying to make gorm to take other name for the columns that are autocreated: created_at, updated_at, id. I tried what you said but it gave error because for example the column created_at is already especified:
column "created_at" specified more than once)

@smacker
Copy link
Contributor

smacker commented May 21, 2017

Just don't embed default moded in your struct. If you look how it's defined you will see:

type Model struct {
	ID        uint `gorm:"primary_key"`
	CreatedAt time.Time
	UpdatedAt time.Time
	DeletedAt *time.Time `sql:"index"`
}

You can define this fields in your own model or create you own base model with different names and embed it.

@jinzhu jinzhu closed this as completed Feb 12, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants