Home > Backend Development > Golang > GORM: Define multiple columns with the same foreign key

GORM: Define multiple columns with the same foreign key

WBOY
Release: 2024-02-13 11:45:16
forward
1128 people have browsed it

GORM: Define multiple columns with the same foreign key

php editor Baicao brings you a question about GORM today: How to use the same foreign key to define multiple columns in GORM? In database design, sometimes we need to use the same foreign key column in multiple tables, which requires us to make appropriate definitions and configurations in GORM. Next, we will introduce in detail how to implement this requirement in GORM, as well as related considerations. Let’s explore this interesting topic together!

Question content

I am creating a Golang MySQL project using GORM. I have a table called accounts which contains field

ID        uint      `json:"id" gorm:"primary_key;auto_increment;not_null"`
    Name      string    `json:"name"`
    Company   string    `json:"company"`
    GSTIN     string    `json:"gstin"`
    AccountNo string    `json:"accountNo" gorm:"unique"`
    IFSC      string    `json:"ifsc"`
    CreatedAt time.Time `json:"createdAt"`
    UpdatedAt time.Time `json:"updatedAt"`
Copy after login

Now I want to make a table named Transactions with fields

ID            uint      `json:"id" gorm:"primary_key;auto_increment;not_null"`
    Amount        float64   `json:"amount"`
    CreatedAt     time.Time `json:"createdAt"`
    UpdatedAt     time.Time `json:"updatedAt"`
    Date          time.Time `json:"Date"`
    PaymentMode   string    `json:"paymentMode"`
    SourceId      uint      `json:"source"`   ------>>>>> Want this to be AccountID foreign key
    UTR           string    `json:"utr" gorm:"uniqueIndex"`
    DestinationId uint      `json:"to"`       ------>>>>> Want this to be AccountID foreign key
    Account       Account
Copy after login

I don't know how to define it in go Gorm? Can I have two fields with foreign keys to the same column of another table? How do i do this?

Solution

Is this done? Thanks!

type Debit struct {
    ID                 uint      `json:"id" gorm:"primary_key;auto_increment;not_null"`
    Amount             float64   `json:"amount"`
    CreatedAt          time.Time `json:"createdAt"`
    UpdatedAt          time.Time `json:"updatedAt"`
    PaymentMode        string    `json:"paymentMode"`
    SourceId           uint      `json:"sourceId"`
    UTR                string    `json:"utr" gorm:"uniqueIndex"`
    DestinationId      uint      `json:"destinationId"`
    SourceAccount      Account   `gorm:"foreignKey:SourceId"`
    DestinationAccount Account   `gorm:"foreignKey:DestinationId"`
}
Copy after login

The above is the detailed content of GORM: Define multiple columns with the same foreign key. For more information, please follow other related articles on the PHP Chinese website!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template