Skip to content

ORM&Mysql : or { panic(err) } #24184

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

Open
Avey777 opened this issue Apr 11, 2025 · 5 comments
Open

ORM&Mysql : or { panic(err) } #24184

Avey777 opened this issue Apr 11, 2025 · 5 comments
Labels
Bug This tag is applied to issues which reports bugs.

Comments

@Avey777
Copy link

Avey777 commented Apr 11, 2025

Describe the bug

Refer to under code

Reproduction Steps

module main

import db.mysql
import time


fn db_mysql() !mysql.DB {
	mut mysql_config := mysql.Config{
		host:     '127.0.0.1'
		port:     3306
		username: 'root'
		password: 'mysql_123456'
		dbname:   'vcore'
	}
	mut conn := mysql.connect(mysql_config) or { return err }
	return conn
}


@[table: 'sys_users']
struct User {
pub:
	id           string  @[immutable; primary; sql: 'id'; sql_type: 'VARCHAR(255)'; unique]
	name         ?string    @[immutable; sql: 'name'; sql_type: 'VARCHAR(255)'; unique]
	created_at   ?time.Time @[omitempty; sql_type: 'TIMESTAMP' ]
	updated_at   time.Time @[default:'0001-01-01T00:00:00 +00:00';omitempty; sql_type: 'TIMESTAMP']
}


fn main() {
	db := db_mysql() or { panic('failed to connect to database') }

	sql db {
	create table User
  } or { panic(err) }

  mut result := sql db {
  	select table User
  } or { panic(err) }
  dump(result)
}

Expected Behavior

[temp.v:80] json.encode(result): []

Current Behavior

V panic: db.mysql.SQLError: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '-01-01T00:00:00 +00:00 NOT NULL, PRIMARY KEY(id), UNIQUE(id), UNIQUE(name)' at line 1 (1) (CREATE TABLE IF NOT EXISTS sys_users (id VARCHAR(255) NOT NULL, name VARCHAR(255), created_at TIMESTAMP, updated_at TIMESTAMP DEFAULT 0001-01-01T00:00:00 +00:00 NOT NULL, PRIMARY KEY(id), UNIQUE(id), UNIQUE(name));)
v hash: d54a823
/tmp/v_1000/orm_m.01JRHFA3Y4FXTHY7G7GXW61374.tmp.c:6642: at _v_panic: Backtrace
/tmp/v_1000/orm_m.01JRHFA3Y4FXTHY7G7GXW61374.tmp.c:12537: by main__main
/tmp/v_1000/orm_m.01JRHFA3Y4FXTHY7G7GXW61374.tmp.c:12746: by main

Possible Solution

No response

Additional Information/Context

No response

V version

V 0.4.10 d54a823

Environment details (OS name and version, etc.)

V full version V 0.4.10 86536e4.d54a823
OS linux, Deepin 23
Processor 6 cpus, 64bit, little endian, Intel(R) Core(TM) i5-8500B CPU @ 3.00GHz
Memory 0.17GB/7.61GB
V executable /home/Jengro/.vmr/versions/v_versions/v_latest/v
V last modified time 2025-04-11 00:14:32
V home dir OK, value: /home/Jengro/.vmr/versions/v_versions/v_latest
VMODULES OK, value: /home/Jengro/.vmodules
VTMP OK, value: /tmp/v_1000
Current working dir OK, value: /home/Jengro
Git version git version 2.45.2
V git status weekly.2025.14-33-gd54a8232
.git/config present true
cc version cc (Deepin 12.3.0-17deepin8) 12.3.0
gcc version gcc (Deepin 12.3.0-17deepin8) 12.3.0
clang version Deepin clang version 17.0.6 (5deepin5)
tcc version tcc version 0.9.28rc 2025-02-13 HEAD@f8bd136d (x86_64 Linux)
tcc git status thirdparty-linux-amd64 696c1d84
emcc version N/A
glibc version ldd (Debian GLIBC 2.38-6deepin7) 2.38

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.

@Avey777 Avey777 added the Bug This tag is applied to issues which reports bugs. label Apr 11, 2025
Copy link

Connected to Huly®: V_0.6-22570

@Avey777
Copy link
Author

Avey777 commented Apr 11, 2025

module main

import db.sqlite
import time

@[table: 'sys_users']
struct Users {
	id         string     @[immutable; primary; sql: 'id'; sql_type: 'VARCHAR(255)'; unique]
	name       ?string    @[immutable; sql: 'name'; sql_type: 'VARCHAR(255)'; unique]
	created_at ?time.Time @[omitempty; sql_type: 'TIMESTAMP']
	updated_at ?time.Time @[omitempty; sql_type: 'TIMESTAMP']
}

type Any = string | int | []string | []int | bool | []map[string]Any | time.Time

fn main() {
	mut db := sqlite.connect(':memory:')!
	defer { db.close() or {} }

	sql db {
		create table Users
	}!
	
	data_all := sql db {
		select from Users where name == '123'
	}!
	dump(data_all)

}

[temp.v:79] data_all: []

This is normal in SQLite

@jorgeluismireles
Copy link

For sqlite, your example works, except if you change update_at field attrs, from:

updated_at ?time.Time @[omitempty; sql_type: 'TIMESTAMP']

to:

updated_at ?time.Time @[default:'0001-01-01T00:00:00 +00:00';omitempty; sql_type: 'TIMESTAMP']

the definition you use for your mysql case.

================ V panic ================
   module: main
 function: main()
  message: near "-": syntax error (1) (CREATE TABLE IF NOT EXISTS `sys_users` (`id` VARCHAR(255) NOT NULL, `name` VARCHAR(255), `created_at` TIMESTAMP, `updated_at` TIMESTAMP DEFAULT 0001-01-01T00:00:00 +00:00, PRIMARY KEY(`id`), UNIQUE(`id`), UNIQUE(`name`));)

@Avey777
Copy link
Author

Avey777 commented Apr 12, 2025

updated_at ?time.Time @[default: now; omitempty; sql_type: 'TIMESTAMP']

This is OK!
I understood it

@Avey777 Avey777 closed this as completed Apr 12, 2025
@Avey777
Copy link
Author

Avey777 commented Apr 12, 2025

I think this should still be enabled, and it should return an error instead of a panic.

@Avey777 Avey777 reopened this Apr 12, 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.
Projects
None yet
Development

No branches or pull requests

2 participants