r/programming • • Jul 12 '26 • Bravo 1

Prefer STRICT tables in SQLite

https://evanhahn.com/prefer-strict-tables-in-sqlite/
358 Upvotes

108 comments sorted by

View all comments

Show parent comment

-24

u/taw Jul 12 '26

The world is full of untyped data, and you need to be able to store it. JSON, CSV, XML, and so on, that's likely vast majority of data out there, in very loosely typed formats. Forcing type checks on data insertion is totally not viable.

Making this a default, that's an interesting choice. Usually you need to opt-in some special column type like JSON, VARIANT or whatever; or store such data as TEXT.

55

u/ric2b Jul 12 '26

Yes, the issue is obviously with making that the default.

13

u/AyrA_ch Jul 12 '26

It's like the default of MySQL to silently throw excessive data away if it doesn't fits the column. I wonder how often this "feature" only got caught in production

2

u/Lonsdale1086 Jul 12 '26

From what I've seen it'll only ever convert a value if it can be done lossless, and failing that it'll store the data as is regardless of type?

I.e "123" -> 123 in an int column, 123 -> 123.0 in a float column, but if you say, tried 123.45 in an int column it'd just store the 123.45 as is.

Which is still scuffed, I can't imagine not wanting an error there, but I'm fine with the concept of an "any" column, but personally I'd do like Key: string(256), Value: string(max), Type:string(120)

Like anything (reasonable) can be losslessly stored as a string if you know what you're doing to serialise and deserialize.

7

u/AyrA_ch Jul 12 '26

For MySQL the behavior really depends on what version you initially installed. Version before MySQL 5.7.5 would silently* truncate data unless the STRICT_TRANS_TABLES setting was explicitly enabled. Version 5.7.5 changed the default to be enabled, but this has no effect if you migrate from an earlier version.

* It will print a warning but will otherwise continue executing as if everything was ok.

2

u/Lonsdale1086 Jul 12 '26

Right, that's very horrific then hahaha.

1

u/TwoWeeks90DaysTops Jul 13 '26

This is a feature that is so much a product of its time. It's the robustness principle.

I think most people has gone away from this way of thinking since it caused a lot of security issues.

7

u/ric2b Jul 13 '26

Throwing half of what was sent to you away does not meet the robustness principle.

1

u/TwoWeeks90DaysTops Jul 13 '26

Haha! Yes, good point.

2

u/ByronScottJones Jul 15 '26

I would instead call that the "I'm too lazy to sanitize my data or check my return values" principle. People that can't be bothered deserve to have crashes.

30

u/inkjod Jul 12 '26

Forcing type checks on data insertion is totally not viable.

...says who?

Data cleanup will eventually become necessary; best to do it early on.

If you don't want to, keep those JSON/CSV/XML/whatever as they are, or store them as BLOBs or TEXT. (So, yeah, ultimately we agree.)

BTW, if you're using XML for untyped data as claimed, you're holding it very wrong.

-26

u/taw Jul 12 '26

You don't get to choose what data exists in the real world.

Anyway, every database system supports this in some way.

1

u/inkjod Jul 13 '26

I see your point. I'm sorry about the ridiculous downvotes; they are not from me.

32

u/Ravek Jul 12 '26

Nah, the world is full of typed data that subsequently gets its type erased when stored in formats that don’t have the capability to model the world more accurately.

1

u/Absolute_Enema Jul 13 '26

In which arbitrary type system?

24

u/mattsowa Jul 12 '26

That makes no sense. You can always have a text column if you don't want to parse it further, nothing is forcing you to do the actual data cleaning.

-17

u/taw Jul 12 '26

Text column is one way to make dynamically typed column, but it has many downsides.

Like you can't even check if two values are equal if you store them like that, as one source might be "0" and another be "0.0". Is it equal or not?

There are whole languages that go "let's pretend every scalar value is text" like Perl, bash, Tcl, or (mostly) Javascript, and it's such a mess.

→ More replies (6)

10

u/busyHighwayFred Jul 12 '26

Json does differentiate between numbers and text though?

5

u/taw Jul 12 '26

JSON typing is total BS too, people just ignore it.

Like if you load {"x": 2} and {"x": 2.0} in Javascript, they're considered completely equivalent.

But if you load it into Python or Ruby, you get completely different types for first and second example!

You get nonsense like some JSON libraries converting language's native big integer type to JSON strings, as that's the only way to ensure they won't get mutilated on the way, as anything in JSON processing chain can randomly decide to force numbers down to double precision floats.

And Javascript has native bigints, but they raise TypeError: Do not know how to serialize a BigInt.

JSON spec doesn't even try to address it.

3

u/tesfabpel Jul 14 '26

SQLite has ANY type just for that (even in STRICT mode).