jovandeginste / workout-tracker

A workout tracking web application for personal use (or family, friends), geared towards running and other GPX-based activities
Other
958 stars 30 forks source link

constraint failed: NOT NULL constraint failed: workouts.checksum (1299) #33

Closed pixelspark closed 8 months ago

pixelspark commented 8 months ago

@jovandeginste I am seeing this error now when adding a workout for GPX files that used to work. Could be related to your recent change that separated the GPX data to a separate table?

jovandeginste commented 8 months ago

The GPX file is not yet added before?

pixelspark commented 8 months ago

It was but I deleted the workout. Also note it is not the UNIQUE constraint failing but the NOT NULL.

Maybe this is the culprit? https://github.com/jovandeginste/workout-tracker/blob/75d8a131593c5c13b87aaea60b052edbec479699/pkg/database/workouts.go#L78

pixelspark commented 8 months ago

Also, note it errors on workouts.checksum, not the GPX table. So apparently gorm still has a not null constraint on workouts.checksum after migration!

jovandeginste commented 8 months ago

No, I think I need to do a cascade delete when a workout is deleted. I will look into this tomorrow; you can check the database manually via a sqlite cli tool.

jovandeginste commented 8 months ago

Also, note it errors on workouts.checksum, not the GPX table. So apparently gorm still has a not null constraint on workouts.checksum after migration!

O, right! That's good to know... I did have that issue too, and tried again and again until I got the issue resolved for me. Apparently, it's not always resolved...

jovandeginste commented 8 months ago

FYI, the nil is not the issue; that's the way you calculate a checksum :)

pixelspark commented 8 months ago

Yep, it has the NOT NULL constraint in the wrong place:

sqlite> select * from sqlite_master where name='workouts';
+-------+----------+----------+----------+--------------------------------------------------------------+
| type  |   name   | tbl_name | rootpage |                             sql                              |
+-------+----------+----------+----------+--------------------------------------------------------------+
| table | workouts | workouts | 5        | CREATE TABLE "workouts"  (`id` integer PRIMARY KEY AUTOINCRE |
|       |          |          |          | MENT,`created_at` datetime,`updated_at` datetime,`deleted_at |
|       |          |          |          | ` datetime,`name` text NOT NULL,`date` datetime NOT NULL,`us |
|       |          |          |          | er_id` integer NOT NULL,`dirty` numeric,`notes` text,`type`  |
|       |          |          |          | text,`data` text,`checksum` blob NOT NULL,`gpx_data` mediumt |
|       |          |          |          | ext,`filename` text,CONSTRAINT `fk_users_workouts` FOREIGN K |
|       |          |          |          | EY (`user_id`) REFERENCES `users`(`id`))                     |
+-------+----------+----------+----------+--------------------------------------------------------------+

Is there such a thing as a gorm:"null" annotation to force it to allow NULL?

jovandeginste commented 8 months ago

You could look at this page: https://gorm.io/docs/migration.html#Constraints

pixelspark commented 8 months ago

FYI, the nil is not the issue; that's the way you calculate a checksum :)

I see the h.Write above it now. If you don't know the Go API for hashing it looks funny I guess.

jovandeginste commented 8 months ago

I was thinking about using the DropConstraint, but then it just worked for me, so I left it as it was.

pixelspark commented 8 months ago

The easy way out would be to set Checksum to an empty array:

Checksum: []byte{},
pixelspark commented 8 months ago

Ah nevermind, that causes issues with the (apparently also still existing) unique constraint...

jovandeginste commented 8 months ago

What would that solve?

jovandeginste commented 8 months ago

The best fix is to use the drop constraint; if you want to continue with other tests, you can just write a random string as checksum (there is code for that already)

pixelspark commented 8 months ago

What would that solve?

It solves the NOT NULL error as an empty array is probably not the same as null (depends on how gorm translates empty arrays to SQL and how SQLite works, but generally with e.g. strings in SQL there is a difference between '' and NULL).

It does not solve the issue however because there is still a UNIQUE constraint on the checksum. And having '' more than once is not allowed still.

pixelspark commented 8 months ago

For now I can just drop the database, it creates it anew without the constraints:

sqlite> select * from sqlite_master where name = 'workouts';
+-------+----------+----------+----------+--------------------------------------------------------------+
| type  |   name   | tbl_name | rootpage |                             sql                              |
+-------+----------+----------+----------+--------------------------------------------------------------+
| table | workouts | workouts | 8        | CREATE TABLE `workouts` (`id` integer PRIMARY KEY AUTOINCREM |
|       |          |          |          | ENT,`created_at` datetime,`updated_at` datetime,`deleted_at` |
|       |          |          |          |  datetime,`name` text NOT NULL,`date` datetime NOT NULL,`use |
|       |          |          |          | r_id` integer NOT NULL,`dirty` numeric,`notes` text,`type` t |
|       |          |          |          | ext,`data` text,`gpx_data` mediumtext,`filename` text,`check |
|       |          |          |          | sum` blob,CONSTRAINT `fk_users_workouts` FOREIGN KEY (`user_ |
|       |          |          |          | id`) REFERENCES `users`(`id`))                               |
+-------+----------+----------+----------+--------------------------------------------------------------+
jovandeginste commented 8 months ago

Yes our comments crossed, sorry

jovandeginste commented 8 months ago

Luckily we are still at <V1 😅

jovandeginste commented 8 months ago

Should be fixed now, but feel free to test!