Gorm: Creating foreign key with migrator

Created on 7 Sep 2020  Â·  7Comments  Â·  Source: go-gorm/gorm

Is there options to create a foreign key using automigrate?

stale missing reproduction steps

Most helpful comment

This is already supported in V2.

@jinzhu What does "this" refer to in this sentence? Can you do AddForeignKey in v2? I do not think so. Are there some docs or examples that provide a bit more extensive info on how to setup FKs in V2?

All 7 comments

This issue has been automatically marked as stale as it missing playground pull request link, checkout https://github.com/go-gorm/playground for details, it will be closed in 2 days if no further activity occurs. if you are asking question, please use the Question template, most likely your question already answered https://github.com/go-gorm/gorm/issues or described in the document https://gorm.io ✨ Search Before Asking ✨

@jinzhu the link doesn't say much. What if you don't want to use the automatic foreign key creation (using an embedded struct) and you want to add one manually like in V1?

// gorm v1
db.AutoMigrate(&TableA{}, &TableB{})
db.Model(TableAB{}).
  AddForeignKey("a_id", "table_a(id)", "CASCADE", "CASCADE").
  AddForeignKey("b_id", "table_b(id)", "CASCADE", "CASCADE")

@jinzhu Are you going to answer the question or at least update the docs to provide a bit more specificity on the matter. Also can this issue be reopened as it does not look like it has been fully resolved?

This is already supported in V2.

This is already supported in V2.

@jinzhu What does "this" refer to in this sentence? Can you do AddForeignKey in v2? I do not think so. Are there some docs or examples that provide a bit more extensive info on how to setup FKs in V2?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

satb picture satb  Â·  3Comments

Quentin-M picture Quentin-M  Â·  3Comments

Ganitzsh picture Ganitzsh  Â·  3Comments

leebrooks0 picture leebrooks0  Â·  3Comments

koalacxr picture koalacxr  Â·  3Comments