v icon indicating copy to clipboard operation
v copied to clipboard

cgen: mutable + array + database = error

Open enghitalo opened this issue 2 years ago • 3 comments

V doctor:

OS: linux, Ubuntu 22.04.1 LTS
Processor: 8 cpus, 64bit, little endian, 11th Gen Intel(R) Core(TM) i7-11390H @ 3.40GHz
CC version: cc (Ubuntu 11.3.0-1ubuntu1~22.04) 11.3.0

getwd: /home/hitalo/Documents/bugs no v/mut_error
vmodules: /home/hitalo/.vmodules
vroot: /home/hitalo/v
vexe: /home/hitalo/v/v
vexe mtime: 2022-11-07 15:12:13
is vroot writable: true
is vmodules writable: true
V full version: V 0.3.2 bf00e9b.5f33585

Git version: git version 2.34.1
Git vroot status: weekly.2022.45 (5 commit(s) behind V master)
.git/config present: true
thirdparty/tcc status: thirdparty-linux-amd64 12f392c3

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

module main

import sqlite

[table: 'taskx']
struct Task {
mut:
	id            u32    [primary; serial; sql: serial]
	frequency     string [required; sql_type: 'varchar(10)']
	deadline_hhmm string [required; sql_type: 'varchar(5)']
}

pub fn create_db_connection() !sqlite.DB {
	mut db := sqlite.connect('databasx.db')!
	return db
}

fn main() {
	mut db := create_db_connection() or { panic(err) }

	sql db {
		create table Task
	} or { panic('error on create table: $err') }

	mut tasks_model := [Task{
		frequency: 'a'
		deadline_hhmm: 'b'
	}]

	for mut task_model in tasks_model {
		sql db {
			insert task_model into Task
		} or { eprintln('Error on insert tasks: \n$err') }
	}

	// sql db {
	// 	insert tasks_model[0] into Task
	// } or { eprintln('Error on insert tasks: \n$err') }
}

What did you expect to see?

task_model inserction

What did you see instead?

==================
/tmp/v_1000/main.10870584657395213853.tmp.c:42359: error: struct or union expected
...
==================
(Use `v -cg` to print the entire error message)

builder error: 
==================
C error. This should never happen.

This is a compiler bug, please report it using `v bug file.v`.

https://github.com/vlang/v/issues/new/choose

You can also use #help on Discord: https://discord.gg/vlang

enghitalo avatar Nov 08 '22 20:11 enghitalo

It looks like the error has since been updated to now say error: can only insert variables, where the datatype of tasks_model[0] is an expression. Should the orm support inserting expressions directly?

jacksonmowry avatar Mar 07 '24 22:03 jacksonmowry

Hi, @jacksonmowry. Are you facing a problem like that right now? Can you try using an indexed variable ?

enghitalo avatar Mar 07 '24 23:03 enghitalo

Oh no, I was just working through some of the issues in the backlog seeing which ones we can close.

It looks likes the original error you had has at least been translated into a V error now, which is better. From the new error message I assume this is the intended behavior, but I was planning to ask.

jacksonmowry avatar Mar 07 '24 23:03 jacksonmowry