RetroFed Forum
Your home away from modern life.
Prefer STRICT tables in SQLite
Post Reply
AuthorMessage
codeinabox programming.dev codeinabox@programming.dev
Joined: 2025-09-29
Quote

9   Original
Azura The Spellkissed lemmy.blahaj.zone AzuraTheSpellkissed@lemmy.blahaj.zone
Joined: 2026-03-30
#2 · Quote Reply

I wasn't even aware, good to know!

tl;dr without strict, sqlite allows setting a columns datatype to unimplemented, bogus types. Also it allows writing values of any type into any column. In strict mode, you can still set columns to the ANY type of you actually want that in some places.

Tap for spoiler, relevant code blocks of the article
-- Non-strict tables let you put anything anywhere.
CREATE TABLE people_nonstrict (age INTEGER);
INSERT INTO people_nonstrict (age) VALUES ('garbage');
-- => works fine

-- Strict tables don't allow that, which I prefer.
CREATE TABLE people_strict (age INTEGER) STRICT;
INSERT INTO people_strict (age) VALUES ('garbage');
-- => error: cannot store TEXT value in INTEGER column

-- Notably, if a value can be losslessly converted, it will still be accepted.
-- These two lines are equivalent, even for a strict table:
INSERT INTO people_strict (age) VALUES ('123');
INSERT INTO people_strict (age) VALUES (123);

-- SQLite doesn't support these types, but this is all accepted.
CREATE TABLE tbl (name GARBAGE);
CREATE TABLE tbl (name DATETIME);
-- ...

-- All of these give errors, which I prefer.
CREATE TABLE tbl (name GARBAGE) STRICT;
CREATE TABLE tbl (name DATETIME) STRICT;
-- ...

-- Still allows flexibility with ANY
CREATE TABLE tbl (value ANY) STRICT;

-- All of these are valid because the column is ANY:
INSERT INTO tbl (value) VALUES (123);
INSERT INTO tbl (value) VALUES ('text');
INSERT INTO tbl (value) VALUES (12.34);
INSERT INTO tbl (value) VALUES (X'8647');
5
Post Reply
Back to top