Golang SQLC无法生成结构体。

huangapple go评论86阅读模式
英文:

Golang SQLC not generating structs

问题

我正在使用sqlc和mysql,由于某种原因,生成的代码在执行查询时没有为要插入或查询数据库的对象的函数中包含结构体或字段。

我的创建表查询类似于这样:

CREATE TABLE users (
    user_id int PRIMARY KEY,
    f_name varchar(255) NOT NULL,
    m_name varchar(255),
    l_name varchar(255) NOT NULL
);

查询看起来像这样:

-- name: CreateUser :exec
INSERT INTO users (f_name,
                   m_name,
                   l_name)
VALUES ($1, $2, $3);

-- name: GetUserByUserId :one
SELECT * FROM users
WHERE user_id = $1;

这是生成的代码:

const createUser = `-- name: CreateUser :exec
INSERT INTO users (
                   f_name,
                   m_name,
                   l_name)
VALUES ($1, $2, $3)
`

func (q *Queries) CreateUser(ctx context.Context) error {
	_, err := q.db.ExecContext(ctx, createUser)
	return err
}

const getUserByUserId = `-- name: GetUserByUserId :one
SELECT f_name, m_name, l_name FROM users
WHERE user_id = $1
`

func (q *Queries) GetUserByUserId(ctx context.Context) (User, error) {
	row := q.db.QueryRowContext(ctx, getUserByUserId)
	var i User
	err := row.Scan(
		&i.UserID,
		&i.FName,
		&i.MName,
		&i.LName
	)
	return i, err
}

从生成的代码中可以看出,createUser函数没有生成相应的结构体,因此无法将其作为参数传递给CreateUser函数。同样,getUserByUserId函数中没有传递userId参数给GetUserByUserId函数。

不确定我做错了什么,这是我的sqlc.yaml文件的内容:

version: "1"
packages:
  - name: "db"
    path: "./db/sqlc"
    queries: "./db/query/"
    schema: "./db/migration/"
    engine: "mysql"
    emit_json_tags: true
    emit_prepared_queries: false
    emit_interface: false
    emit_exact_table_names: false
英文:

I am using sqlc and mysql, for some reason the generated code that is meant to execute the queries doesn't have the structs or fields in the functions for the objects am supposed to insert or query the db with.

My create table query looks similar to this

CREATE TABLE users (
    user_id int PRIMARY KEY,
    f_name varchar(255) NOT NULL,
    m_name varchar(255),
    l_name varchar(255) NOT NULL
);

The queries look like this

-- name: CreateUser :exec
INSERT INTO users (f_name,
                   m_name,
                   l_name)
VALUES ($1, $2, $3);

-- name: GetUserByUserId :one
SELECT * FROM users
WHERE user_id = $1;

This is what is generated

const createUser = `-- name: CreateUser :exec
INSERT INTO users (
                   f_name,
                   m_name,
                   l_name)
VALUES ($1, $2, $3)
`

func (q *Queries) CreateUser(ctx context.Context) error {
	_, err := q.db.ExecContext(ctx, createUser)
	return err
}

const getUserByUserId = `-- name: GetUserByUserId :one
SELECT f_name, m_name, l_name FROM users
WHERE user_id = $1
`

func (q *Queries) GetUserByUserId(ctx context.Context) (User, error) {
	row := q.db.QueryRowContext(ctx, getUserByUserId)
	var i User
	err := row.Scan(
		&i.UserID,
		&i.FName,
		&i.MName,
		&i.LName
	)
	return i, err
}

As you can see from in the code generated for createUser there is no struct generated that can in turn be passed a function parameter to CreateUser, the same for getUserByUserId there is no userId parameter being passed in the GetUserByUserId function.

Not certain what am doing wrong, here is how my sqlc.yaml file looks like

version: "1"
packages:
  - name: "db"
    path: "./db/sqlc"
    queries: "./db/query/"
    schema: "./db/migration/"
    engine: "mysql"
    emit_json_tags: true
    emit_prepared_queries: false
    emit_interface: false
    emit_exact_table_names: false

答案1

得分: 1

所以我在sqlc的GitHub存储库中进行了一些研究,并找到了解决方案这里

我的错误是在声明查询参数时使用了$,而mysql不识别。我改用了?,然后它就起作用了。

我的查询现在看起来像这样:

-- name: CreateUser :exec
INSERT INTO users (f_name,
m_name,
l_name)
VALUES (?, ?, ?);

-- name: GetUserByUserId :one
SELECT * FROM users
WHERE user_id = ?;

英文:

So I did a little digging in the issues on the sqlc GitHub repo and found
the solution here

My mistake was I was using $ when stating query parameters which mysql doesn't recognize. I used ? instead and it worked

My queries now looks something like this

-- name: CreateUser :exec
INSERT INTO users (f_name,
                   m_name,
                   l_name)
VALUES (?, ?, ?);

-- name: GetUserByUserId :one
SELECT * FROM users
WHERE user_id = ?;

huangapple
  • 本文由 发表于 2021年9月16日 17:51:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/69206193.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定