v icon indicating copy to clipboard operation
v copied to clipboard

orm: mysql select returning wrong id getting last 3 bytes of next field (title)

Open enghitalo opened this issue 3 years ago • 0 comments

Discord: https://discord.com/channels/592103645835821068/592114487759470596/1002639476499173476

V doctor:

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

getwd: /home/hitalo/Documents/bugs no v
vmodules: /home/hitalo/.vmodules
vroot: /home/hitalo/v
vexe: /home/hitalo/v/v
vexe mtime: 2022-07-29 17:11:25
is vroot writable: true
is vmodules writable: true
V full version: V 0.3.0 f1ebfb2.8268df7

Git version: git version 2.34.1
Git vroot status: weekly.2022.30-24-g8268df7e-dirty
.git/config present: true
thirdparty/tcc status: Error: fatal: detected dubious ownership in repository at '/home/hitalo/v/thirdparty/tcc'
To add an exception for this directory, call:

	git config --global --add safe.directory /home/hitalo/v/thirdparty/tcc
 Error: fatal: detected dubious ownership in repository at '/home/hitalo/v/thirdparty/tcc'
To add an exception for this directory, call:

	git config --global --add safe.directory /home/hitalo/v/thirdparty/tcc


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

module main

import vweb
import zztkm.vdotenv
import mysql
import os

const (
	http_port = 8081
)

struct App {
	vweb.Context
}

[table: 'task']
struct Task {
mut:
	id    u32    [primary; serial; sql: serial]
	title string [required; sql_type: 'varchar(191)']
}

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

	defer {
		db.close()
	}

	sql db {
		create table Task
	}
	task_model := Task{
		title: 'Terminar relatóabc'
	}

	sql db {
		insert task_model into Task
	}

	results := sql db {
		select from Task
	}
	println('results: $results')
	assert results[0].id == 0

	vweb.run_at(new_app(), vweb.RunParams{
		host: '192.168.0.6'
		family: .ip
	}) or { panic(err) }
}

fn new_app() &App {
	mut app := &App{}

	return app
}

pub fn create_db_connection() ?mysql.Connection {
	mut db := mysql.Connection{
		host: os.getenv('DB_HOST')
		port: os.getenv('DB_PORT').u32()
		username: os.getenv('DB_USERNAME')
		password: os.getenv('DB_PASSWORD')
		dbname: os.getenv('DB_NAME')
	}

	db.connect() or { println(err) }

	return db
}

What did you expect to see?

[Vweb] Running app on http://192.168.0.6:8080/

What did you see instead?

query: SELECT `id`, `title` FROM `task`;
results: [Task{
    id: 6513249
    title: 'Terminar relatóabc'
}, Task{
    id: 6513249
    title: 'Terminar relatóabc'
}, Task{
    id: 6513249
    title: 'Terminar relatóabc'
}, Task{
    id: 6513249
    title: 'Terminar relatóabc'
}]
wrong_id_bug.v:46: FAIL: fn main.main: assert results[0].id == 0
   left value: results[0].id = 6513249
  right value: 0
V panic: Assertion failed...
v hash: 8268df7
/tmp/v_1000/wrong_id_bug.2771298222559068708.tmp.c:16735: at _v_panic: Backtrace
/tmp/v_1000/wrong_id_bug.2771298222559068708.tmp.c:52273: by main__main
/tmp/v_1000/wrong_id_bug.2771298222559068708.tmp.c:53534: by main

enghitalo avatar Jul 29 '22 18:07 enghitalo