Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BelongsTo relationship generates wrong xID type when using UUID #115

Closed
aslakknutsen opened this issue Jun 21, 2016 · 0 comments · Fixed by #118
Closed

BelongsTo relationship generates wrong xID type when using UUID #115

aslakknutsen opened this issue Jun 21, 2016 · 0 comments · Fixed by #118

Comments

@aslakknutsen
Copy link
Contributor

aslakknutsen commented Jun 21, 2016

Given:

Model("Identity", func() {
    Description("")
    Field("id", gorma.UUID, func() {
        PrimaryKey()
        SQLTag("type:uuid")
        Description("This is the ID PK field")
    })
    Field("full_name", gorma.String, func() {
        Description("The fullname of the Identity")
        Alias("fullName")
    })
    Field("image_url", gorma.String, func() {
        Description("The image URL for this Identity")
        Alias("imageURL")
    })
})
Model("User", func() {
    Description("")
    Field("id", gorma.UUID, func() {
        PrimaryKey()
                SQLTag("type:uuid")
        Description("This is the ID PK field")
    })
    Field("email", gorma.String, func() {
        Description("This is the unique email field")
    })
    BelongsTo("Identity")
})

Expected:

type Identity struct {
    ID        uuid.UUID `sql:"type:uuid" gorm:"primary_key"` // This is the ID PK field
        ...
    FullName  string `gorm:"column:fullName"` // The fullname of the Identity
    ImageURL  string `gorm:"column:imageURL"` // The image URL for this Identity
}

type User struct {
    ID        uuid.UUID `sql:"type:uuid" gorm:"primary_key"` // This is the ID PK field
       ...
    IdentityID uuid.UUID   // Belongs To Identity
    Identity   Identity
}

But got:

type User struct {
    ID        uuid.UUID `sql:"type:uuid" gorm:"primary_key"` // This is the ID PK field
       ...
    IdentityID int   // Belongs To Identity  // INT ??
    Identity   Identity
}
aslakknutsen added a commit to aslakknutsen/gorma that referenced this issue Jul 11, 2016
Instead of hard-coding the Type of a BelongsTo|HasManyKey|HasOneKey relationship
to be in, resolve the target model in the relationship and use its PrimaryKey as
xID type in source struct.

Note: Currently this will panic if multiple PrimaryKeys are defined.

fixes goadesign#115
aslakknutsen added a commit to aslakknutsen/gorma that referenced this issue Jul 11, 2016
aslakknutsen added a commit to aslakknutsen/gorma that referenced this issue Jul 12, 2016
aslakknutsen added a commit to aslakknutsen/gorma that referenced this issue Jul 12, 2016
BelongsTo(parent, tags ...string) used as SQLTags on
generated BelongsToID field.

Used to e.g. override gorm to generate native pg uuid type
in target table.

fixes goadesign#115
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant