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

321

u/vivekkhera Jul 12 '26

I always found it incredible that the default in SQLite has been to allow any data in any column. It just doesn’t make sense to me. I’m glad they have a way to disable that misfeature.

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.

→ More replies (2)

19

u/DoingItForEli Jul 12 '26

avoiding that kind of validation is what makes it lite I guess lol

-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

→ More replies (7)

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.

→ More replies (4)

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.

→ More replies (2)

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.

→ More replies (7)

10

u/busyHighwayFred Jul 12 '26

Json does differentiate between numbers and text though?

→ More replies (2)
→ More replies (1)
→ More replies (1)