I
I
Ilya2015-03-25 02:35:20
SQL
Ilya, 2015-03-25 02:35:20

GORM: Why is an error thrown when creating a structure and saving it to the sqlite database with a ForeignKey, but with a different name in the column?

Used by SQLite.
There are 2 structures:

type Compaing struct {
  Id            int
  Name          string `sql:"type:varchar(250)"`
  Groups        []Group
}

type Group struct {
  Id                 int
  Name               string `sql:"type:varchar(1500)"`
  GroupIdq           string `sql:"type:varchar(250)"`
  CompaingId         int `gorm:"column:to_compaing_id"`
}

In the Group structure, in the CompaingId column, there is a ForeignKey on Compaing. When creating a new company and including new groups at once, an error is thrown. I create like this:
Compaing{
      {
        Name:      "New compaing",
        Available: true,
        Deleted:   false,
        Groups: []Group{
          {
            Name: "123",
          },
        },
      },
    },

It does not want to write to the database, it gives an error - could not convert column to field.
If we remove `gorm:"column:to_compaing_id"` from the Group structure, then everything works fine. But I can't change the structure of the db.
If you change CompaingId to ToCompaingId, then it does not write new groups at all, it does not even give an error, apparently it does not associate this field with the company key.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question