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

ORM produces errors and panics when using anonymous structs #23299

Open
islonely opened this issue Dec 28, 2024 · 5 comments
Open

ORM produces errors and panics when using anonymous structs #23299

islonely opened this issue Dec 28, 2024 · 5 comments
Labels
Bug This tag is applied to issues which reports bugs. ORM Bugs/feature requests, that are related to the V ORM.

Comments

@islonely
Copy link
Contributor

islonely commented Dec 28, 2024

Describe the bug

cgen errors and a V panic is produced when trying to insert/select a struct with anonymous struct children.

Reproduction Steps

This does the cgen error.

import db.sqlite

fn main() {
    mut db := sqlite.connect(':memory:')!
    sql db {
        create table Foo
    }!
    foo := Foo {
        name: struct{
            first: "John",
            last: "Smith"
        }
    }
    sql db {
        insert foo into Foo
    }!
    foos := sql db {
        select from Foo
    } or {
        []Foo{}
    }
    println(foo)
    println(foos)
}

struct Foo {
    id int @[primary; serial]
    name struct {
        first string
        last string
    }
}

This does the V panic.

import db.sqlite

fn main() {
    mut db := sqlite.connect(':memory:')!
    sql db {
        create table Foo
    }!
    foo := Foo {
        name: struct{
            first: "John",
            last: "Smith"
        }
    }
    sql db {
        insert foo into Foo
    }!
    foos := sql db {
        select from Foo
    } or {
        []Foo{}
    }
    println(foo)
    println(foos)
}

struct Foo {
    id int @[primary; serial]
    name struct {
        id int @[primary; serial]
        // ^ THIS LINE FIXED THE CGEN ERROR
        first string
        last string
    }
}

Expected Behavior

Expected a V error letting me know I cannot use anonymous structs or to use @[skip] on the anonymous struct field.

Current Behavior

For the first example in reproduction steps:

cgen error: field "id" does not exist on "main._VAnonStruct1"

For the second:

V panic: result not set (no such table: _VAnonStruct1 (1) (INSERT INTO `_VAnonStruct1` (`first`, `last`) VALUES (?1, ?2);))
v hash: b487986
0   test                                0x0000000102a838b0 panic_result_not_set + 124
1   test                                0x0000000102a98294 main__main + 1308
2   test                                0x0000000102a99414 main + 84
3   dyld                                0x000000019c020274 start + 2840

Possible Solution

Make the ORM create a table for anonymous structs when sql db { create table Foo } is used.

Additional Information/Context

No response

V version

V 0.4.9 2911f29.b487986

Environment details (OS name and version, etc.)

V full version: V 0.4.9 2911f29.b487986
OS: macos, macOS, 15.1.1, 24B91
Processor: 8 cpus, 64bit, little endian, Apple M3

getwd: /Users/adamoates/Documents/shattlebip
vexe: /Users/adamoates/v/v
vexe mtime: 2024-12-28 17:47:59

vroot: OK, value: /Users/adamoates/v
VMODULES: OK, value: /Users/adamoates/.vmodules
VTMP: OK, value: /tmp/v_501

Git version: git version 2.39.5 (Apple Git-154)
Git vroot status: 0.4.9-40-gb487986b
.git/config present: true

CC version: Apple clang version 16.0.0 (clang-1600.0.26.4)
emcc version: N/A
thirdparty/tcc status: thirdparty-macos-arm64 713692d4

Note

You can use the 👍 reaction to increase the issue's priority for developers.

Please note that only the 👍 reaction to the issue itself counts as a vote.
Other reactions and those to comments will not be taken into account.

Huly®: V_0.6-21731

@islonely islonely added the Bug This tag is applied to issues which reports bugs. label Dec 28, 2024
@Delta456
Copy link
Member

what is Foo?

@JalonSolov
Copy link
Contributor

Foo is defined at the bottom of both examples.

There may be some confusion here... the title talks about anonymous structs, but a named struct is used.

@Delta456
Copy link
Member

Yes, it is confusing

@spytheman
Copy link
Member

the anonymous struct is inside the Foo one, see the name struct { field.

@Delta456
Copy link
Member

Do we need a checker error for this?

@felipensp felipensp added the ORM Bugs/feature requests, that are related to the V ORM. label Jan 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug This tag is applied to issues which reports bugs. ORM Bugs/feature requests, that are related to the V ORM.
Projects
None yet
Development

No branches or pull requests

5 participants