fraenky8 / tables-to-go

convert your database tables to structs easily
MIT License
233 stars 42 forks source link

database/sql import not being set on models #33

Closed rshingleton closed 2 years ago

rshingleton commented 3 years ago

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" }

fraenky8 commented 3 years ago

Hi @rshingleton,

based on your structs, I created following tables:

CREATE TABLE ActionAuditLog (
    Type VARCHAR(255) NOT NULL,
    User VARCHAR(255) NOT NULL,
    Details VARCHAR(255),
    Time DATETIME NOT NULL DEFAULT CURRENT_TIME
);

CREATE TABLE MonitorSnapshot (
    SystemID VARCHAR(255) NOT NULL,
    Name VARCHAR(255) NOT NULL,
    Time DATETIME,
    Data VARCHAR(255) NOT NULL
);

CREATE TABLE SeriesData (
    SeriesID INT NOT NULL,
    StartTime INT NOT NULL,
    EndTime INT NOT NULL,
    Value VARCHAR(255),
    Weight VARCHAR(255)
);

and ran the latest master branch of tables-to-go, which produced the following structs. Code comments are added by me for explanations. MariaDB version 10.2.9-MariaDB-10.2.9+maria~jessie.

package dto

import (
    "database/sql" // imported because of `sql.NullString`
    "time"
)

type ActionAuditLog struct {
    Type    string         `db:"Type"`
    User    string         `db:"User"`
    Details sql.NullString `db:"Details"`
    Time    time.Time      `db:"Time"`
}
package dto

import (
    // imported because of `mysql.NullTime` which is basically a reference to `sql.NullTime`.
    // Improvement of this tool would be to use sql.NullTime directly and avoid this import to be more consistent.
    "github.com/go-sql-driver/mysql" 
)

type MonitorSnapshot struct {
    SystemID string         `db:"SystemID"`
    Name     string         `db:"Name"`
    Time     mysql.NullTime `db:"Time"`
    Data     string         `db:"Data"`
}
package dto

import (
    "database/sql"  // imported because of `sql.NullString`
)

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"`
}

Can you explain once more what is not working for you?

fraenky8 commented 2 years ago

Closing this with the note, that fro now on sql.NullTime is used instead of the database driver specific null-time in https://github.com/fraenky8/tables-to-go/pull/38