-
-
Save kyanny/b94392239e66b938aea27e0990b697ce to your computer and use it in GitHub Desktop.
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
GOPATH=/Users/kensuke.nagae/go/1.14.4 #gosetup | |
/Users/kensuke.nagae/.anyenv/envs/goenv/versions/1.14.4/bin/go build -o /private/var/folders/tq/4t_ymlx56mv9fsvpqdwvvhf40000gr/T/___go_build_scratch_53_go /Users/kensuke.nagae/Library/Application Support/JetBrains/GoLand2020.2/scratches/scratch_53.go #gosetup | |
/private/var/folders/tq/4t_ymlx56mv9fsvpqdwvvhf40000gr/T/___go_build_scratch_53_go | |
&main.Address{ID:0, UserID:1, ZipCode:"6040835", State:"京都府", City:"京都市", Line1:"中京区御池通間之町東入高宮町206", Line2:"御池ビル9F"} | |
&main.Address{ID:1, UserID:1, ZipCode:"6040835", State:"京都府", City:"京都市", Line1:"中京区御池通間之町東入高宮町206", Line2:"御池ビル9F"} | |
2020/12/21 19:26:51 pq: insert or update on table "address_log" violates foreign key constraint "address_log_address_id_fk" | |
Process finished with exit code 1 |
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" | |
"log" | |
_ "github.com/lib/pq" | |
) | |
type Address struct { | |
ID int64 | |
UserID int64 | |
ZipCode string | |
State string | |
City string | |
Line1 string | |
Line2 string | |
} | |
type AddressLog struct { | |
ID int64 | |
UserID int64 | |
AddressID int64 | |
ZipCode string | |
State string | |
City string | |
Line1 string | |
Line2 string | |
} | |
func setup() (*sql.Tx, func()) { | |
db, err := sql.Open("postgres", "postgres://postgres@localhost/postgres?sslmode=disable") | |
if err != nil { | |
log.Fatal(err) | |
} | |
db.Exec(`drop table address_log`) | |
db.Exec(`drop table address`) | |
db.Exec(`create table public.address | |
( | |
id serial not null | |
constraint address_pk | |
primary key, | |
user_id integer not null, | |
zip_code text not null, | |
state text not null, | |
city text not null, | |
line1 text not null, | |
line2 text not null | |
); | |
alter table public.address owner to vandle_api; | |
`) | |
db.Exec(`create table public.address_log | |
( | |
id serial not null | |
constraint address_log_pk | |
primary key, | |
address_id integer not null | |
constraint address_log_address_id_fk | |
references public.address, | |
user_id integer not null, | |
zip_code text not null, | |
state text not null, | |
city text not null, | |
line1 text not null, | |
line2 text not null | |
); | |
alter table public.address_log owner to vandle_api; | |
`) | |
tx, err := db.Begin() | |
if err != nil { | |
log.Fatal(err) | |
} | |
_, err = tx.Exec(`insert into address (user_id, zip_code, state, city, line1, line2) values ($1, $2, $3, $4, $5, $6)`, 1, "6040835", "京都府", "京都市", "中京区御池通間之町東入高宮町206", "御池ビル9F") | |
if err != nil { | |
log.Fatal(err) | |
} | |
return tx, func() { | |
err = tx.Rollback() | |
if err != nil { | |
log.Fatal(err) | |
} | |
} | |
} | |
func UpdateAddress(tx *sql.Tx, userID int64, zipCode, state, city, line1, line2 string) error { | |
a := &Address{} | |
err := tx.QueryRow(`select user_id, zip_code, state, city, line1, line2 from address where user_id = $1`, userID).Scan(&a.UserID, &a.ZipCode, &a.State, &a.City, &a.Line1, &a.Line2) | |
if err != nil { | |
return err | |
} | |
fmt.Printf("%#v\n", a) | |
aa := &Address{} | |
err = tx.QueryRow(`select id, user_id, zip_code, state, city, line1, line2 from address where user_id = $1`, userID).Scan(&aa.ID, &aa.UserID, &aa.ZipCode, &aa.State, &aa.City, &aa.Line1, &aa.Line2) | |
if err != nil { | |
return err | |
} | |
fmt.Printf("%#v\n", aa) | |
_, err = tx.Exec(`insert into address_log (user_id, address_id, zip_code, state, city, line1, line2) values ($1, $2, $3, $4, $5, $6, $7)`, a.UserID, a.ID, a.ZipCode, a.State, a.City, a.Line1, a.Line2) | |
if err != nil { | |
return err | |
} | |
_, err = tx.Exec(`update address set (user_id, zip_code, state, city, line1, line2) values($1, $2, $3, $4, $5, $6)`, userID, zipCode, state, city, line1, line2) | |
if err != nil { | |
return err | |
} | |
return nil | |
} | |
func main() { | |
tx, cleanup := setup() | |
defer cleanup() | |
err := UpdateAddress(tx, 1, "1070062", "東京都", "港区", "南青山6-5-55", "青山サンライトビル3F") | |
if err != nil { | |
log.Fatal(err) | |
} | |
err = tx.Commit() | |
if err != nil { | |
log.Fatal(err) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment