postgresql - 使用 xo 从 postgres 数据库表模式生成 DTO 结构

标签 postgresql go

我只是想知道是否有人使用过 xo .我有一个 Postgres 数据库,里面有一堆表和东西。

我似乎无法使用 xo 从 postgres 表结构生成 DTO struct

这个内置的xo template似乎创建了一个 struct,我相信 xo 默认使用内置模板,但我在运行时得到的都是

xo --verbose --suffix '.go' pgsql://myusername:mypass@localhost:myport/mydb?sslmode=disable -o internal/qo/models -p qo

是一堆与我的项目或我的数据库中的模式完全无关的文件。 Screenshot of one of the files and the entire list of generated files on the left

最佳答案

所以根据我的发现,xo包裹的 postgreSQL loader使用“public”作为默认模式,在我的数据库中,名为“public”的模式只有名为 schema_migrations 的表和很多存储过程,从而导致上述一组适当生成的代码。

关于使用--schema <correct_schema_name>其中 <correct_schema_name>是要为其生成 dto 结构的架构,则生成正确的代码。

所以,正确的命令是:

xo --verbose --schema <correct_schema_name> --suffix '.go' pgsql://myusername:mypass@localhost:myport/mydb?sslmode=disable -o internal/qo/models -p qo

gen.sh 中还有一些设置。还必须更改的文件,例如类型信息(特别是默认值),以便生成具有正确字段类型的结构。

例如,时间戳的默认类型是 pq.NullTime (pq 是 PostgreSQL 的 Golang 驱动程序),将在 gen.sh 中更改文件以便设置正确的默认类型。

关于postgresql - 使用 xo 从 postgres 数据库表模式生成 DTO 结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54996599/

相关文章:

使用 Heroku dataclips 将 PostgreSQL 数据库导出到 csv 时出现 SQL 语法错误

go - 正确的 Go 类型传递给 C 函数?

go - 如何构建一个可以使用 Go 向多个客户端接收和发送消息的服务器?

go - 日志repo的抽象实现

sql - 我可以通过哪些方式提高 PostgreSQL 8 中正则表达式查询的性能?

sql - 列的默认值(当值设置为 NULL 时)等于其他列

Django 查询集

postgresql - 在 pg 转储后插入到表中

xml - 无法使用 golang 解码 XML,始终为空结构

go - Jmoiron SQLX Golang通用接口(interface)