'Can the gorm column tag specify more than one field name? Or can case be ignored?

Now I'm using two tables with the same field name but different casing. But I just want to define a struct to deserialize these two mysql table fields? Can it be done?

e.g.

table1:

 create table users1(
            username varchar (65) not null,
            password varchar (65) not null
        )

table2:

 create table users2(
            userName varchar (65) not null,
            passWord varchar (65) not null
        )

However, only one struct can be defined, e.g.:

type User struct {
            userName   string    `gorm:"column:username"`   
            PassWord   string    `gorm:"column:passeord"`
        }

How should I define a struct? Or can it be done in some other way???



Solution 1:[1]

When you define a struct it means that this structure will belongs only to one table, so in your case decision is create another struct for second table

type UserOne struct {
   username string `gorm:"column:username"
   password string `gorm:"column:password"
}

type UserTwo struct {
   username string `gorm:"column:userName"
   password string `gorm:"column:passWord"
}

if you will migrate these both structures gorm will create 2 tables user_one and user_two, if you want to operate with already existing tables you can change it names with gorm func, here the example How to set singular name for a table in gorm

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ilya Vasilev