[mysql] How to use generated UUID as id #1080

Open
opened 2018-08-23 09:09:21 +00:00 by eloo · 5 comments
eloo commented 2018-08-23 09:09:21 +00:00 (Migrated from github.com)

Hi,
i'm currently trying to use a UUID as id for my entity.
I want the database to create the UUID but it looks like this is not possible?

I'm using mysql so i guess a trigger should be created but i didn't found out how to do this.
At the moment i'm trying something like

Id string `xorm:"pk UUID 'id'"`

but UUID are still not created..

Maybe you guys have any ideas how to achieve this?
Thanks

PS: maybe its related to #502

Hi, i'm currently trying to use a UUID as id for my entity. I want the database to create the UUID but it looks like this is not possible? I'm using mysql so i guess a trigger should be created but i didn't found out how to do this. At the moment i'm trying something like ``` Id string `xorm:"pk UUID 'id'"` ``` but UUID are still not created.. Maybe you guys have any ideas how to achieve this? Thanks PS: maybe its related to #502

It didn't support mysql but postgres I think.

It didn't support mysql but postgres I think.
eloo commented 2018-08-23 11:05:22 +00:00 (Migrated from github.com)

okay.. so i need to do a workaround like

for _, val := range entryMap {
		count, err = engine.Before(func(bean interface{}) {
			item := bean.(*models.Entry)
			item.Id = uuid.NewV4().String()
		}).Insert(&val)
	}

???

okay.. so i need to do a workaround like ``` for _, val := range entryMap { count, err = engine.Before(func(bean interface{}) { item := bean.(*models.Entry) item.Id = uuid.NewV4().String() }).Insert(&val) } ``` ???

Yeah, that works. And another way is that you could also add a mehtod to all your structs.

type BaseUUID struct {
    Id string
}

func (b *BaseUUID) BeforeInsert() {
    b.Id = uuid.NewV4().String()
}

type MyTable struct {
     BaseUUID `xorm:"extends"`
}
Yeah, that works. And another way is that you could also add a mehtod to all your structs. ```Go type BaseUUID struct { Id string } func (b *BaseUUID) BeforeInsert() { b.Id = uuid.NewV4().String() } type MyTable struct { BaseUUID `xorm:"extends"` } ```
eloo commented 2018-08-23 15:40:07 +00:00 (Migrated from github.com)

@lunny oh that looks way better than my hack :D
i will try this soon
thx

@lunny oh that looks way better than my hack :D i will try this soon thx
eloo commented 2018-08-24 12:26:51 +00:00 (Migrated from github.com)

@lunny
okay yes it works and i ended up with

newEntities := make([]*models.Entity, 0)
for key := range entityMap {
	val := entityMap[key]
	newEntities = append(newEntities, &val)
}
count, err := engine.Insert(&newEntities)

and

type BaseUUID struct {
	Id string `xorm:"UUID pk"`
}

func (b *BaseUUID) BeforeInsert() {
	b.Id = uuid.NewV4().String()
}

type Entity struct {
	BaseUUID `xorm:"extends"`
}

but i still need the workaround with the pointers for the insert statement..

@lunny okay yes it works and i ended up with ``` newEntities := make([]*models.Entity, 0) for key := range entityMap { val := entityMap[key] newEntities = append(newEntities, &val) } count, err := engine.Insert(&newEntities) ``` and ``` type BaseUUID struct { Id string `xorm:"UUID pk"` } func (b *BaseUUID) BeforeInsert() { b.Id = uuid.NewV4().String() } type Entity struct { BaseUUID `xorm:"extends"` } ``` but i still need the workaround with the pointers for the insert statement..
Sign in to join this conversation.
No Milestone
No Assignees
1 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: xorm/xorm#1080
No description provided.