diff --git a/README.md b/README.md index ccab06db..b8b23416 100644 --- a/README.md +++ b/README.md @@ -1116,6 +1116,21 @@ type Product struct { } ``` +## Composite Foreign Primary Integer Keys + +```go +type ProductStore struct { + ProductID int64 `gorm:"primary_key;foreignkey"` + StoreID int64 `gorm:"primary_key;foreignkey"` + Quantity int +} +``` + +When you declare a gorm primary key as foreign, there is likely no good use case for also auto incrementing the field. +This disables the auto increment behavior when creating the table from gorm. + + + ## Database Indexes & Foreign Key ```go diff --git a/model_struct.go b/model_struct.go index 10423ae2..676bdbde 100644 --- a/model_struct.go +++ b/model_struct.go @@ -350,6 +350,10 @@ func (scope *Scope) generateSqlTag(field *StructField) string { autoIncrease = true } + if field.IsPrimaryKey && field.IsForeignKey { + autoIncrease = false + } + sqlType = scope.Dialect().SqlTag(reflectValue, size, autoIncrease) }