Last active
August 29, 2015 14:00
-
-
Save jordan-wright/11377812 to your computer and use it in GitHub Desktop.
Test of Gorm's Foreign Key Functionality
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"database/sql" | |
"fmt" | |
"time" | |
"github.com/jinzhu/gorm" | |
_ "github.com/mattn/go-sqlite3" | |
) | |
type User struct { | |
Id int64 | |
Birthday time.Time | |
Age int64 | |
Name string `sql:"size:255"` | |
CreatedAt time.Time | |
UpdatedAt time.Time | |
DeletedAt time.Time | |
Emails []Email // Embedded structs | |
BillingAddress Address // Embedded struct | |
BillingAddressId sql.NullInt64 // BillingAddress's foreign key | |
ShippingAddress Address // Another Embedded struct with same type | |
ShippingAddressId int64 // ShippingAddress's foreign key | |
IgnoreMe int64 `sql:"-"` // Ignore this field | |
} | |
type Address struct { | |
Id int64 | |
Address1 string `sql:"not null;unique"` // Set this field as not nullable and unique in database | |
Address2 string `sql:"type:varchar(100);unique"` | |
Post sql.NullString `sql:not null` | |
// FYI, "NOT NULL" will only work well with NullXXX Scanner, because golang will initalize a default value for most type... | |
} | |
type Email struct { | |
Id int64 | |
UserId int64 // Foreign key for User | |
Email string `sql:"type:varchar(100);"` // Set this field's type | |
Subscribed bool | |
} | |
func main() { | |
db, err := gorm.Open("sqlite3", "test.db") | |
if err != nil { | |
panic(fmt.Sprintf("Got error when connect database, the error is '%v'", err)) | |
} | |
user := User{ | |
Name: "jinzhu", | |
BillingAddress: Address{Address1: "Billing Address - Address 1"}, | |
ShippingAddress: Address{Address1: "Shipping Address - Address 1"}, | |
Emails: []Email{{Email: "[email protected]"}, {Email: "jinzhu-2@[email protected]"}}, | |
} | |
db.CreateTable(User{}) | |
db.CreateTable(Email{}) | |
db.CreateTable(Address{}) | |
err = db.Debug().Save(user).Error | |
if err != nil { | |
fmt.Println(err) | |
} | |
} | |
/* Output | |
main.go:59 [2014-04-28 11:56:03] INSERT INTO addresses ("address1","address2","post") VALUES ('Billing Address - Address 1','','{ false}') | |
main.go:59) column address2 is not unique | |
main.go:59)[2014-04-28 11:56:03] INSERT INTO addresses ("address1","address2","post") VALUES ('Shipping Address - Address 1','','{ false}') | |
main.go:59)[2014-04-28 11:56:03] INSERT INTO users ("birthday","age","name","created_at","updated_at","deleted_at","billing_address_id","shipping_address_id") VALUES ('0001-01-01 0 | |
0:00:00 +0000 UTC','0','jinzhu','0001-01-01 00:00:00 +0000 UTC','0001-01-01 00:00:00 +0000 UTC','0001-01-01 00:00:00 +0000 UTC','{0 false}','0') | |
main.go:59)[2014-04-28 11:56:03] INSERT INTO emails ("user_id","email","subscribed") VALUES ('0','[email protected]','false') | |
main.go:59)[2014-04-28 11:56:03] INSERT INTO emails ("user_id","email","subscribed") VALUES ('0','jinzhu-2@[email protected]','false') | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment