Merge 8545f588249455f922d415a699e0526c779a1639 into 27a442b5ecef401a3ba2325383a96a09b0a983d9

This commit is contained in:
Yan-Fa Li 2015-07-02 18:11:04 +00:00
commit 2dbafd7729
2 changed files with 19 additions and 0 deletions

View File

@ -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 ## Database Indexes & Foreign Key
```go ```go

View File

@ -350,6 +350,10 @@ func (scope *Scope) generateSqlTag(field *StructField) string {
autoIncrease = true autoIncrease = true
} }
if field.IsPrimaryKey && field.IsForeignKey {
autoIncrease = false
}
sqlType = scope.Dialect().SqlTag(reflectValue, size, autoIncrease) sqlType = scope.Dialect().SqlTag(reflectValue, size, autoIncrease)
} }