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

[ENHANCEMENT REQUEST / BUG] - Support multi-level struct embed column concatenation for struct models #643

Closed
dborowiec10 opened this issue Aug 8, 2022 · 0 comments · Fixed by #655
Labels
enhancement New feature or request

Comments

@dborowiec10
Copy link

When embedding structs, I have been following this guide: https://bun.uptrace.dev/guide/models.html#embedding-structs

This works great and enables to have concatenated column names given the struct as follows (per official example):

type Role struct {
	Name     string
	Users    Permissions `bun:"embed:users_"`
	Profiles Permissions `bun:"embed:profiles_"`
	Roles    Permissions `bun:"embed:roles_"`
}

type Permissions struct {
	View   bool
	Create bool
	Update bool
	Delete bool
}

producing the following table:

CREATE TABLE roles (
    name TEXT,

    users_view BOOLEAN,
    users_create BOOLEAN,
    users_update BOOLEAN,
    users_delete BOOLEAN,

    profiles_view BOOLEAN,
    profiles_create BOOLEAN,
    profiles_update BOOLEAN,
    profiles_delete BOOLEAN,

    roles_view BOOLEAN,
    roles_create BOOLEAN,
    roles_update BOOLEAN,
    roles_delete BOOLEAN
);

However, when attempting to create multi-level nested struct embeds like so:

type Role struct {
	Name     string
	Users    Permissions `bun:"embed:users_"`
	Profiles Permissions `bun:"embed:profiles_"`
	Roles    Permissions `bun:"embed:roles_"`
}

type Permissions struct {
	View   Permission   `bun:"embed:view_"`
	Create Permission   `bun:"embed:create_"`
	Update Permission   `bun:"embed:update_"`
	Delete Permission   `bun:"embed:delete_"`
}

type Permission struct {
       Enabled bool     `bun:"enabled"`
       Metadata string  `bun:"metadata"`
}

bun produces the following table:

CREATE TABLE roles (
    name TEXT,

    view_enabled BOOLEAN,
    view_metadata TEXT,

    create_enabled BOOLEAN,
    create_metadata TEXT,

    update_enabled BOOLEAN,
    update_metadata TEXT,

    delete_enabled BOOLEAN,
    delete_metadata TEXT,
);

which is ambiguous as to which permission is going to be persisted as part of the model (Users, Profiles, Roles?).

I would have expected bun to produce the following table:

CREATE TABLE roles (
    name TEXT,

    users_view_enabled BOOLEAN,
    users_view_metadata TEXT,
    users_create_enabled BOOLEAN,
    users_create_metadata TEXT,
    users_update_enabled BOOLEAN,
    users_update_metadata TEXT,
    users_delete_enabled BOOLEAN,
    users_delete_metadata TEXT,

    profiles_view_enabled BOOLEAN,
    profiles_view_metadata TEXT,
    profiles_create_enabled BOOLEAN,
    profiles_create_metadata TEXT,
    profiles_update_enabled BOOLEAN,
    profiles_update_metadata TEXT,
    profiles_delete_enabled BOOLEAN,
    profiles_delete_metadata TEXT,

    roles_view_enabled BOOLEAN,
    roles_view_metadata TEXT,
    roles_create_enabled BOOLEAN,
    roles_create_metadata TEXT,
    roles_update_enabled BOOLEAN,
    roles_update_metadata TEXT,
    roles_delete_enabled BOOLEAN,
    roles_delete_metadata TEXT,
);

I am uncertain whether this is by design and not currently supported (in which case my issue would be an enhancement request) or a bug (as there is nothing in the documentation explicitly stating that multi-level nested structs are not supported for the embed tag).

@vmihailenco vmihailenco added the enhancement New feature or request label Aug 9, 2022
mcdoker18 added a commit to mcdoker18/bun that referenced this issue Aug 17, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants