Skip to content
Discussion options

You must be logged in to vote
import db.sqlite

@[table: 'users']
struct User {
	id        int @[primary; sql: serial]
	user_name string
	children  []Role @[fkey: 'user_id'; skip]
}

@[table: 'roles']
struct Role {
	id        int @[primary; sql: serial]
	user_id   int
	role_name string
}

fn main() {
	db := sqlite.connect(':memory:')!

	sql db {
		create table User
		create table Role
	}!

	user := User{
		user_name: 'parent-1'
	}
	sql db {
		insert user into User
	}!

	role := Role{
		user_id:   1
		role_name: 'role-1'
	}
	sql db {
		insert role into Role
	}!

	// 4️⃣ 查询父记录(不自动联查 children)
	users := sql db {
		select from User
	}!
	println('父表查询结果: ${users}')

	roles := sql db {
		select from Role
	}!
	println('子表查询结…

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by Avey777
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
1 participant