stephenafamo/bob

t.Insert fails to scan result for tables with TIMESTAMP columns in MySQL 8

Closed this issue · 2 comments

I'm trying to use bob to insert items into a MySQL 8 table with a TIMESTAMP column.

The table looks like this:

CREATE TABLE item (
	id SERIAL PRIMARY KEY,
	name VARCHAR(255) NOT NULL,
	created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL
)

Code generation runs successfully with bobgen-mysql. Then, when I run t.Insert it returns an error.

The insert code:

item, err := models.Items.Insert(ctx, db, &models.ItemSetter{
	Name: omit.From("ITEM"),
})

And the error message:

failed to insert item: sql: Scan error on column index 2, name "created_at": unsupported Scan, storing driver.Value type []uint8 into type *time.Time

Despite the scan error for the result, the database insert is successful.

Here is a full example: https://github.com/llamux/bob-mysql-timestamps

To reproduce run:

docker-compose up -d
# wait for db init
go run ./cmd/main

The error is returned from this function in opt. And it makes sense because there is no handling for []uint8 -> time.Time as far as I can tell. I'm not sure what should be done in bob to handle this or whether there is desire to handle this at all.

If this is user error and I should be doing this differently with bob, please let me know.

How do your DSN look like? Did you remember to add ?parseTime=true?

See https://github.com/go-sql-driver/mysql#parsetime

Came back to comment after finding this: https://github.com/go-sql-driver/mysql?tab=readme-ov-file#timetime-support

?parseTime=true fixed my issue.

@jacobmolby Thank you!