fraenky8 / tables-to-go Goto Github PK
View Code? Open in Web Editor NEWconvert your database tables to structs easily
License: MIT License
convert your database tables to structs easily
License: MIT License
Hi, thanks for great repo.
I wonder if it is possible if we can specify which table(s) that we'll be convert into struct.
maybe using flag table
?
and also option to define the generated filename, such as model.go
example 1: tables-to-go -d booking_app -table invoice -of /internal/domain/invoice -o model
--> will create file /internal/domain/invoice/model.go
example 2: tables-to-go -d booking_app -table order -of /internal/domain/order -o model
--> will create file /internal/domain/order/model.go
I think this would allow us to be more selective on generating the struct and saving it into each domain folder with specified filename.
i already use tabbles-to-go with myql, so thanks ,
, now i want use it with sqlite ,can you support it?
add option to generate json tag
I always use unix socket and disable network in my local machine. it'd be great if this tool support to connect to mysql trough unix socket ๐
Noticed it generates:
Id int `db:"id"`
It should be
ID int `db:"id"`
For idiomatic Go IIUC
Is there a plan to support the use of gureu/null?
Hey!
In postgres if I have a column as:
+--------------------------+--------------------------+-----------+
| Column | Type | Modifiers |
|--------------------------+--------------------------+-----------|
| rule_severity | severity | not null |
where severity
is a type CREATE TYPE severity AS ENUM ('low', 'medium', 'high', 'critical');
tables-to-go
will generate: RuleSeverity *string
I was expecting RuleSeverity string
since we have a not null
column.
I will see if I can have a PR for this (if this is simple!).
Hi, possibly a weird feature request, but would it be reasonable to add a -force-null
flag to force all fields to appear NULL
able?
It seems to be the norm where I work to use sql.Null*
for everything, including for columns that are NOT NULL
. There are some arguments I could give for and against this practice, but in general it seems like it'd be a roadblock (at least at my shop) to adoption of an otherwise perfect developer tool.
I'm guessing it'd be simple enough implementation-wise that I could write a PR for this, but I wanted to test the waters first
Currently the tool stops on the first table that it finds an error on.
It would be nice if there was a flag to continue so we can get structs for all tables that we can, and then we can just examine and manually construct the tables that wouldn't work.
This is a kind feature request :)
Is there any way of generate structs also of database views?
Thank you.
If the column names of a table start with digits, the tool fails to create the struct.
> processing table "MyTable"
> number of columns: 4
> id
> name
> 1person
> 2person
run error: could not create struct file for table MyTable: could not format file /home/ronnie/project/models/MyTable.go: 12:1: expected '}', found 1%
I'm not sure what the best thing to do here would be. I know it's bad practice to have SQL column names start with numbers but it's technically allowed. So we may have to deal with it if we don't have authority to change the actual database schema.
But Golang obviously doesn't allow fieldnames to start with numbers, so what would be a sensible thing to do?
x1person
person1
This may open up a whole can of worms because MySQL table and column names can be full unicode but Go only allows letters, underscore, and digits, and each identifier has to start with a letter or underscore.
Then again, it is really absurd to use non-letter characters in table and column names, so it would probably be fair enough to just not deal with them.
Hi,
Mysql 8 has UPPERCASE columns in information_schema.tables and information_schema.columns therefore it will trigger errors:
could not get tables: missing destination name TABLE_NAME in *[]*database.Table
TABLE_NAME should be lowercase disregarding that in the query is lowercase select table_name ...
However the fix is pretty easy by adding select table_name as 'table_name' from...
in the queries in mysql.go like this:
SELECT table_name as 'table_name' FROM information_schema.tables WHERE table_type = 'BASE TABLE' AND table_schema = ? ORDER BY table_name
and
SELECT ordinal_position as 'ordinal_position', column_name as 'column_name', data_type as 'data_type', column_default as 'column_default', is_nullable as 'is_nullable', character_maximum_length as 'character_maximum_length', numeric_precision as 'numeric_precision', column_key as 'column_key', extra as 'extra' FROM information_schema.columns WHERE table_name = ? AND table_schema = ? ORDER BY ordinal_position
Thank you
First off thanks for the great repo, I'm sure this would be of use to many people.
I wonder if it's possible to get the structs from a SQL definition instead of connecting to the database? I'm thinking it would accept the create table SQL either through a file, stdin or a flag. This would allow a nice web UI like https://mholt.github.io/json-to-go/
Sorry if this has been answered before but I'm a little confused, I ran the go get
command and it installed the binary into my $PATH, but I can't find the source or anything, so idk where to run the Makefile for SQLite3 from.
Thanks.
Generating models for a medium sized database and half the table models end up not getting decorated with database/sql. I can't see any reason in particular, most tables are simple. Some have a single import while others also import "time" or "github.com/go-sql-driver/mysql".
Using latest version installable by go get github.com/fraenky8/tables-to-go as of this morning, 9/21/2021.
Version: latest
Datbase: MariaDB - 10.2.9-MariaDB-log
Examples:
package database
import (
"time"
)
type ActionAuditLog struct {
Type string db:"Type"
User string db:"User"
Details sql.NullString db:"Details"
Time time.Time db:"Time"
}
package database
import (
"github.com/go-sql-driver/mysql"
)
type MonitorSnapshot struct {
SystemID string db:"SystemId"
Name string db:"Name"
Time mysql.NullTime db:"Time"
Data sql.NullString db:"Data"
}
package database
type SeriesData struct {
SeriesID int db:"SeriesId"
StartTime int db:"StartTime"
EndTime int db:"EndTime"
Value sql.NullString db:"Value"
Weight sql.NullString db:"Weight"
}
given table:
CREATE TABLE test (
id integer PRIMARY KEY AUTO_INCREMENT,
name varchar(100) NOT NULL
)
expected output:
type Test struct {
Id int `db:"id"`
Name string `db:"name"`
}
actual output:
type Test struct {
Id int `db:"Id"`
Name string `db:"Name"`
}
The db
-tags Id
and Name
start with upper letter instead of original column name.
Add unit tests!
In MySQL 5.5, tables-to-go
run failed because of datetime_precision
is not exists.
running for "mysql"...
> number of tables: 513
run error could not prepare the get-column-statement: Error 1054: Unknown column 'datetime_precision' in 'field list'%
datetime_precision
option is available from MySQL 5.6.4. https://dev.mysql.com/doc/refman/5.6/en/fractional-seconds.html
In tables-to-go
, datetime_precision
is not used yet (or, any plan to use it?). For supporting MySQL under 5.6, it can be fixed by removing from PrepareGetColumnsOfTableStmt
.
// PrepareGetColumnsOfTableStmt prepares the statement for retrieving the columns of a specific table for a given database
func (mysql *mysql) PrepareGetColumnsOfTableStmt() (err error) {
mysql.getColumnsOfTableStmt, err = mysql.db.Preparex(`
SELECT
ordinal_position,
column_name,
data_type,
column_default,
is_nullable,
character_maximum_length,
numeric_precision,
datetime_precision,
column_key,
extra
FROM information_schema.columns
WHERE table_name = ?
AND table_schema = ?
ORDER BY ordinal_position
`)
return err
}
For example the gen
result https://github.com/unee-t/tables-to-go/blob/bug1/tests/Persons.go#L10 is:
IdPerson int `db:"IdPerson"`
I expected it to be
IdPerson int `db:"id_person"`
Better still would (probably) be:
ID int `db:"id_person"`
I'm aware of -format o
but I don't want my struct names to have underscores in them! ๐ข
Please see:
This is how I gen
the output.
https://github.com/unee-t/tables-to-go/blob/bug1/tests/Persons.go#L14 is such an example
Please see:
This is how I gen
the output.
Given this table
create table foo_has_bar
(
foo_id integer not null
constraint foo_has_bar_foo_id _fk
references foo (id),
bar_id integer not null
constraint foo_has_bar_bar_id_fk
references bar (id),
constraint foo_has_bar_foo_id_bar_id_pk
primary key (foo_id, bar_id)
);
the tool produces the following struct
type FooHasBar struct {
FooId int `db:"FooId"`
FooId int `db:"FooId"`
BarId int `db:"BarId"`
BarId int `db:"BarId"`
}
therefore go reports errors of duplicate fields.
Hi
I'm trying to connect to CockroachDB which uses the same connection DSN string as Postgres, but cannot find any arg parameter to disable the SSL mode! I'm receiving this error message:
pq: SSL is not enabled on the server
NB: I checked the source code, file "pkg/database/postgresql.go", function DSN(), line: 47. The sslmode already set to "disable" which means it should connect without SSL! Is this a bug?!
Any hints please?
Implement one more command line flag to determine which data type to use for NULL
columns:
I run go get,and it report this error:
internal/cli/tables-to-go-cli.go:12:2: //go:build comment without // +build comment
If a schema is say just age INT
I think you set the type sql.NullInt64. But this generates gnarly JSON mappings like
"Age": {
"Int64": 0,
"Valid": true
}
My workaround is to then edit the generated type to for the optional value:
Age int `db:"age" json:"age,omitempty"`
However I've noticed that go sets 0
(ZERO) in my JSON to DB mapping with this "workaround".
INSERT INTO `users` (`id`, `name`, `age`) VALUES
(1, 'Arnie', 0);
If it was the original sql.NullInt64
the value in DB would be:
(2, 'Arnie 2', NULL);
Which is more correct in terms of the DBMS, but then how does one handle the JSON ugliness?!
I also have a type issue when it comes to required values (name): https://s.natalian.org/2019-03-31/required.mp4
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.