v icon indicating copy to clipboard operation
v copied to clipboard

ORM: structs without `id` not supported for querying databases

Open bitcrowd0 opened this issue 2 years ago • 2 comments

V doctor:

OS: linux, "Fedora release 35 (Thirty Five)"
CC version: cc (GCC) 11.2.1 20220127 (Red Hat 11.2.1-9)
V full version: V 0.2.4 74eabba.d7adb67
thirdparty/tcc status: thirdparty-linux-amd64 3654d6a8

What did you do? v -g -o vdbg cmd/v && vdbg db_test.v

module test

import sqlite

fn get_cols(db sqlite.DB) []string {
	r := sql db {
		select from SqliteMaster
	}
	return []
}

[table: 'sqlite_master']
struct SqliteMaster {
	component_type string [sql: 'type']
}

fn test_get_cols(){}

What did you expect to see?

It should be possible to use a struct without an id field for querying the database.

What did you see instead?

db_test.v:7:15: error: V orm: `id int` must be the first field in `test.SqliteMaster`
    5 | fn get_cols(db sqlite.DB) []string {
    6 |     r := sql db {
    7 |         select from SqliteMaster
      |                     ~~~~~~~~~~~~
    8 |     }
    9 |     return []

bitcrowd0 avatar Apr 13 '22 11:04 bitcrowd0

hard code?

changrui avatar Apr 13 '22 12:04 changrui

This is very important when working with associative/junction tables.

merlindiavova avatar May 08 '22 21:05 merlindiavova