Gorm: ์—ด๊ฑฐํ˜• ๋ฌธ์ œ

์— ๋งŒ๋“  2014๋…„ 11์›” 26์ผ  ยท  3์ฝ”๋ฉ˜ํŠธ  ยท  ์ถœ์ฒ˜: go-gorm/gorm

๋‚˜๋Š” ๋‹ค์Œ์„ ๊ฐ€์ง€๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค :

type BufferType string
const (
    Console BufferType = "console"
    Channel BufferType = "channel"
    Conversation BufferType = "conversation"
)

type Buffer struct {
    Bid  int64  `gorm:"primary_key:yes"`
    Type BufferType `sql:"not null;type:ENUM('console', 'channel', 'conversation')"`
    Name string `sql:"size:255;not null"`
}

db.Save(buffer)๋ฅผ ์‹œ๋„ํ•˜๋ฉด ๋‹ค์Œ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค.
sql: converting Exec argument #1's type: unsupported type BufferType, a string)

BufferType์ด ์‹ค์ œ๋กœ ๋ฌธ์ž์—ด์ผ ๋ฟ์ž„์„ ํ•ด๊ฒฐํ•˜๊ณ  ์ €์žฅํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๊นŒ? ์ด ๊ตฌ์กฐ์ฒด๋ฅผ db.Find()๋กœ ๋กœ๋“œํ•˜๋ ค๊ณ  ํ•˜๋ฉด ๋น„์Šทํ•œ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ•ฉ๋‹ˆ๋‹ค.

๊ฐ€์žฅ ์œ ์šฉํ•œ ๋Œ“๊ธ€

๋‚˜๋Š” ๊ฐ™์€ ๋ฌธ์ œ๊ฐ€ ์žˆ์—ˆ๋‹ค. @bramp ์˜ ์†”๋ฃจ์…˜์€ ์—ฌ์ „ํžˆ โ€‹โ€‹๋‚˜์—๊ฒŒ ์˜ค๋ฅ˜๋ฅผ ์ฃผ์—ˆ๋‹ค. ๋‹ค์Œ์€ ๋‚˜๋ฅผ ์œ„ํ•ด ์ผํ–ˆ์Šต๋‹ˆ๋‹ค.

func (u *BufferType) Scan(value interface{}) error { *u = BufferType(value.([]byte)); return nil }
func (u BufferType) Value() (driver.Value, error)  { return string(u), nil }

์ฐจ์ด๋Š” ์‚ฌ์šฉ []byte ๋Œ€์‹  string . ์ด stackoverflow ์Šค๋ ˆ๋“œ๊ฐ€ ์ €๋ฅผ ๋„์™”์Šต๋‹ˆ๋‹ค: http://stackoverflow.com/a/20582504/2418739

๋ชจ๋“  3 ๋Œ“๊ธ€

๋””๋ฒ„๊น…์„ ์ข€ ๋” ํ•ด๋ณด๋‹ˆ ์ด ์˜ค๋ฅ˜๊ฐ€ golang ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค/sql ์ฝ”๋“œ์—์„œ ๋‚˜์˜ค๋Š” ๊ฒƒ ๊ฐ™์€๋ฐ ์—ฌ๊ธฐ ์—์„œ ํ•ด๊ฒฐ์ฑ…์„ ์ฐพ์•˜

func (u *BufferType) Scan(value interface{}) error { *u = BufferType(value.(string)); return nil }
func (u BufferType) Value() (driver.Value, error)  { return string(u), nil }

์•ฝ๊ฐ„ ํˆฌ๋ฐ•ํ•˜์ง€๋งŒ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค๊ฐ€ ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ๋งคํ•‘ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์•„๋Š” ๋ฐ ๋„์›€์ด ๋ฉ๋‹ˆ๋‹ค. ํ•„์š”ํ•œ ๊ฒฝ์šฐ ์—ฌ๊ธฐ์— ์˜ค๋ฅ˜ ๊ฒ€์‚ฌ๋ฅผ ์ถ”๊ฐ€ํ•  ์ˆ˜๋„ ์žˆ์Šต๋‹ˆ๋‹ค.

๋‚˜๋Š” ๊ฐ™์€ ๋ฌธ์ œ๊ฐ€ ์žˆ์—ˆ๋‹ค. @bramp ์˜ ์†”๋ฃจ์…˜์€ ์—ฌ์ „ํžˆ โ€‹โ€‹๋‚˜์—๊ฒŒ ์˜ค๋ฅ˜๋ฅผ ์ฃผ์—ˆ๋‹ค. ๋‹ค์Œ์€ ๋‚˜๋ฅผ ์œ„ํ•ด ์ผํ–ˆ์Šต๋‹ˆ๋‹ค.

func (u *BufferType) Scan(value interface{}) error { *u = BufferType(value.([]byte)); return nil }
func (u BufferType) Value() (driver.Value, error)  { return string(u), nil }

์ฐจ์ด๋Š” ์‚ฌ์šฉ []byte ๋Œ€์‹  string . ์ด stackoverflow ์Šค๋ ˆ๋“œ๊ฐ€ ์ €๋ฅผ ๋„์™”์Šต๋‹ˆ๋‹ค: http://stackoverflow.com/a/20582504/2418739

+1 ํ›Œ๋ฅญํ•œ ์†”๋ฃจ์…˜. ์ด๊ฒƒ์ด GORM ๋ฌธ์„œ์— ์žˆ์—ˆ์œผ๋ฉด ํ•ฉ๋‹ˆ๋‹ค.

์ด ํŽ˜์ด์ง€๊ฐ€ ๋„์›€์ด ๋˜์—ˆ๋‚˜์š”?
0 / 5 - 0 ๋“ฑ๊ธ‰