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

209

u/Nicksaurus Jul 12 '26

The official page about it is mostly nonsense too: https://sqlite.org/flextypegood.html

Some people contend that if you have rigorous constraints on the schema, and especially strict enforcement of column datatypes, this will help prevent incorrect data from being added to the database. This is not true. It is true that type enforcement might help prevent egregiously incorrect data from getting into the system. But type enforcement is no help in prevent subtly incorrect data from being recorded.

"Some people may claim that strict types help avoid entering invalid data. This is not true". Then literally the next sentence explains how they help to avoid entering invalid data

The rest of the page is mostly just examples of why you might want a column to accept multiple types but ignores the question of why that should apply to every column

87

u/hansolo669 Jul 12 '26

Ah yes, the usual arguments against types that conflate data validation with data types. Even the example they give at the bottom is how they got validation wrong. I guess SQL blurs the lines, but that's not quite the argument they're making...

2

u/edgmnt_net Jul 14 '26

Well, you can cover that too, to blur the lines even further, but at least in programming you likely need more advanced stuff like dependent types.

97

u/Nyefan Jul 12 '26

Christ.

I have also written a lot of C code over a span of 35 years, not the least of which is SQLite itself. I have found the type system in C to be very helpful at finding and preventing problems. For the Fossil Version Control System, which is written in C, I have even implemented supplemental static analysis programs that scan the Fossil source code prior to compilation, looking for problems that compilers miss. This works well for compiled programs.

...

Some C-language bugs might have been caught by better type enforcement (which is why I wrote the supplemental source code scanners), but no SQL bugs.

Based on decades of experience, I reject the thesis that rigid type enforcement helps prevent application bugs. I will accept and believe a slightly modified thesis: Rigid type enforcement helps to prevent applications bugs in languages that lack a single top-level "Value" superclass. But SQLite does have the single "sqlite3_value" superclass, so that proverb does not apply.

What is void*.

But also, imagine writing all of your java code just rawdogging Objects. Or writing javascript.

22

u/[deleted] Jul 12 '26

[removed] — view removed comment

32

u/Nyefan Jul 12 '26

If the behavior is the same (as would be required for a drop in replacement), it doesn't really fix the core issue in this thread, yeah?

19

u/[deleted] Jul 12 '26

[removed] — view removed comment

14

u/OphioukhosUnbound Jul 12 '26 edited Jul 16 '26

I'm very excited by Turso* (and its team's approaches to testing it), but how to keep it as a "drop-in" replacement while also evolving past SQLite isn't something to hand-wave. [There do seem to be strict typing improvements and a strict-typing flag. But by default it still respects SQLite's loose and slightly odd typing approaches.]

* encryption, async, logged transactions, and the ability to have a remote DB that is *copied* to local users for local-like speed is awesome.

2

u/sagenumen Jul 15 '26

I’m having trouble parsing your comment(s)

1

u/OphioukhosUnbound Jul 16 '26

I, evidently, had trouble writing a parseable comment!

Just edited the one above.
I dropped helpful words in various places. Sincere apologies for eating brain resources. :)

1

u/lenkite1 Jul 16 '26

Maybe they should give a db option for strict type checking then. Turso currently doesn't do that - you still need to add STRICT to every CREATE TABLE statement. And several other SQLite footguns were also copied for "compatibility".

20

u/standard_revolution Jul 13 '26

It is a classic black/white argument as can be seen about any improvement ever: Well seatbelts don't protect me when a pipe falls down from the truck in front of me!!! seatbelts are worthless!!!

A lot of people don't seem to get that preventing mistakes is always a layered approach, with no layer being perfect

0

u/Absolute_Enema Jul 13 '26 edited Jul 13 '26

Except the static type system isn't a seatbelt (which objectively doesn't create any constraint to driving a car).

It's more like a suit of armor, you can totally argue that it protects you but the usefulness of that protection is debatable and it'll make driving the car more difficult.

8

u/Unfair-Sleep-3022 Jul 12 '26

Also this is the freaking database.

Let application developers handle type conversions if needed.

Who knows where the data is even coming from.

-2

u/Absolute_Enema Jul 14 '26

Because every column is liable to changing in ways that a static type system cannot accomodate without breaking changes.

7

u/ByronScottJones Jul 15 '26

If for some reason I decided that my field that stores INTs now suddenly needs to store haikus as well, I feel like it's okay if that's a breaking change that requires forethought.