There are 2 structures

type Student struct { Id uint64 `sql:"AUTO_INCREMENT" gorm:"primary_key"` Name string `sql:"size:80` } type Struct1 struct { Id uint64 `sql:"AUTO_INCREMENT" gorm:"primary_key"` StudentId Student //`gorm:"ForeignKey:Id"` Time time.Time `sql:"DEFAULT:current_timestamp"` Value int //КакиС-Π½ΠΈΠ±ΡƒΠ΄ΡŒ значСния } 

I did the migration and the tables were created in the database

 db.AutoMigrate(&Student{}) db.AutoMigrate(&Struct1{}) 

But I want, that in a DB communications with cascade change too were created.
That is, now I have added student id 1, and the other table does not know about the existence of the Students table and you can write anything to StudentId.
How to register keys through gorm?

  • In the convention of names in go , ID used instead of Id . But this, I am. - Ivan Black
  • StudentID must be an integer, and the reference to the structure is done separately. Type pastebin - Ivan Black
  • With this code, the tables are not related - Rakzin Roman
  • one
    Use @"ΠΌΠΎΠΉ Π½ΠΈΠΊ" , not commenting on my posts, so that I could see the comment. - Ivan Black
  • Ok.Thanks @IvanBlack - Rakzin Roman

1 answer 1

For example for such models

 // has_many Ys type X struct { gorm.Model } // belongs_to X type Y struct { gorm.Model XID uint } 

In so

 db.Model(&Y{}).AddForeignKey("x_id", "xes (id)", "CASCADE", "CASCADE") 

Respectively RESTRICT instead of CASCADE if necessary. Docks The first CASCADE works ON UPODATE second ON DELETE .

The resulting SQL will be

 ALTER TABLE "ys" -- ΠΊ ΠΊΠ°ΠΊΠΎΠΉ Ρ‚Π°Π±Π»ΠΈΡ†Π΅ добавляСтся ΠΎΠ³Ρ€Π°Π½ΠΈΡ‡Π΅Π½ΠΈΠ΅ ADD CONSTRAINT "ys_x_id_xes_id_foreign" -- имя ограничСния FOREIGN KEY ("x_id") -- какая ΠΊΠΎΠ»ΠΎΠ½ΠΊΠ° Π±ΡƒΠ΄Π΅Ρ‚ внСшним ΠΊΠ»ΡŽΡ‡ΠΎΠΌ (Π² Ρ‚Π°Π±Π»ΠΈΡ†Π΅ ys) REFERENCES xes (id) -- ΠΊ ΠΊΠ°ΠΊΠΎΠΉ Ρ‚Π°Π±Π»ΠΈΡ†Π΅ (ΠΊ ΠΊΠ°ΠΊΠΎΠΉ ΠΊΠΎΠ»ΠΎΠ½ΠΊΠ΅) ON DELETE CASCADE -- ΠΏΡ€ΠΈ ΡƒΠ΄Π°Π»Π΅Π½ΠΈΠΈ ON UPDATE CASCADE; -- ΠΏΡ€ΠΈ ΠΎΠ±Π½ΠΎΠ²Π»Π΅Π½ΠΈΠΈ 

probably will need to update GORM with

 go get -u github.com/jinzhu/gorm 
  • I tried something all the options. There are no restrictions in the database. Can you please write an example? I read on the site. - Rakzin Roman
  • one
    @RakzinRoman, everything works. Here is the pasta . Remember that this restriction does not guarantee that Y will necessarily refer to X. To do this, add NOT NULL to x_id . - Ivan Black
  • one
    @RakzinRoman, I mean that x_id can remain NULL and the restriction will not work in this case. - Ivan Black
  • Oooh .... how to take your code entirely, everything works, and he himself took parts a little bit everywhere .... Thank you - Rakzin Roman
  • @ "Ivan Black" @Ivan Black Writes the relationship "xes" does not exist. I did not understand, but what is here at all xes? db.Model (& Y {}). AddForeignKey ("x_id", "xes (id)", "CASCADE", "CASCADE") - here we are binding the second table (Y) to the field x_id to the field X (id) table X field id. I understood correctly? need exactly with brackets? It’s written on the site, but it’sn’t working for me, I’m doing something wrong ... - Rakzin Roman