Fixed: "sqlite_fail('constraint valid_priority already exists', 1)" After ALTER TABLE ADD CONSTRAINT in SQLite
I replaced the list in a SQLite CHECK constraint with ALTER TABLE ... DROP CONSTRAINT and ADD CONSTRAINT, and the shell printed an empty table headed sqlite_fail('cons.... It looks like an error, but the change had worked. Here's what that header is, why the SQLite 3.54 shell prints it, and how to stop it.
What I Was Doing
I had a tickets table whose priority column allowed only low, medium and high, through a CHECK constraint with IN. I needed to add urgent to the list. SQLite 3.53.0 added DROP CONSTRAINT and ADD CONSTRAINT to ALTER TABLE, so I gave the check a name, valid_priority, and replaced it with a new one. This is the script, saved as replace_check.sql:
CREATE TABLE tickets (
title TEXT NOT NULL,
priority TEXT NOT NULL
CONSTRAINT valid_priority CHECK (priority IN ('low', 'medium', 'high'))
);
INSERT INTO tickets (title, priority) VALUES ('Printer jam', 'low');
ALTER TABLE tickets DROP CONSTRAINT valid_priority;
ALTER TABLE tickets ADD CONSTRAINT valid_priority
CHECK (priority IN ('low', 'medium', 'high', 'urgent'));
INSERT INTO tickets (title, priority) VALUES ('Server down', 'urgent');
SELECT * FROM tickets;

CONSTRAINT valid_priority CHECK (...)gives the check a name, so it can be dropped later.ALTER TABLE tickets DROP CONSTRAINT valid_priority;removes the old list.ALTER TABLE tickets ADD CONSTRAINT valid_priority CHECK (...)adds the new list, withurgentin it.- The second
INSERTusesurgent, which only the new list allows.
I ran it with the sqlite3 shell from SQLite 3.54.1:
sqlite3 -table helpdesk.db ".read replace_check.sql"
sqlite3 opens the database file helpdesk.db, and creates it when it isn't there. -table prints each result as a table, and .read runs the statements in the script.
What I Expected
CREATE TABLE, INSERT and ALTER TABLE change the database and print nothing when they work. Only the SELECT returns rows, so I expected one table: Printer jam at low, and Server down at urgent.
What I Got
+----------------------+
| sqlite_fail('cons... |
+----------------------+
+-------------+----------+
| title | priority |
+-------------+----------+
| Printer jam | low |
| Server down | urgent |
+-------------+----------+

Above the result was an extra, empty table with one column, headed sqlite_fail('cons.... The word "fail" made it look as if the ALTER TABLE had failed. But the shell reports a failed statement with an Error or Parse error line, and there wasn't one. And Server down went in with urgent, which the old list refused.
What I Checked
The new rule was in place. .schema prints a table's definition:
sqlite3 helpdesk.db ".schema tickets"
CREATE TABLE tickets (
title TEXT NOT NULL,
priority TEXT NOT NULL, CONSTRAINT valid_priority
CHECK (priority IN ('low', 'medium', 'high', 'urgent')));
valid_priority now lists urgent. SQLite added it at the end of the table's definition, as a table constraint. A value that isn't on the list was still refused, now by the new rule:
sqlite3 helpdesk.db "INSERT INTO tickets (title, priority) VALUES ('Coffee machine', 'someday');"
Error in 2nd command line argument: CHECK constraint failed: valid_priority
The full header. In a terminal, the shell shortens column titles to 20 characters. .mode table --titlelimit 0 shows them in full, and --rowcount on prints the number of rows under each result. I dropped and added the constraint once more, with both options:
sqlite3 -table helpdesk.db ".mode table --titlelimit 0 --rowcount on" "ALTER TABLE tickets DROP CONSTRAINT valid_priority;" "ALTER TABLE tickets ADD CONSTRAINT valid_priority CHECK (priority IN ('low', 'medium', 'high', 'urgent'));"
+------------------------------------------------------------+
| sqlite_fail('constraint valid_priority already exists', 1) |
+------------------------------------------------------------+
(0 rows)
The full title says the constraint "already exists", the opposite of what happened. (0 rows) shows what the shell received: the ALTER TABLE statement returned a result with one column and no rows.
SQLite's source code. ADD CONSTRAINT is written in src/alter.c. Before it adds a named constraint, it runs an internal query, SELECT sqlite_fail('constraint %q already exists', %d) FROM ..., that looks for a constraint with the same name. sqlite_fail() is an internal function that turns its message into an error, so the query fails only when it finds a row. Then a second query, SELECT sqlite_fail('constraint failed', %d) ..., looks for existing rows that break the new rule. Here both found nothing, so nothing failed.
The 3.54.0 release notes. The release notes say: "For columnar output modes, the CLI now shows the column name header by default even if the query returns no rows." That's the empty table: a header with no rows under it.
Other ALTER TABLE commands. I ran five more on a new, empty table, saved as other_alters.sql:
CREATE TABLE contacts (name TEXT, phone TEXT);
ALTER TABLE contacts ADD COLUMN email TEXT NOT NULL;
ALTER TABLE contacts ALTER COLUMN name SET NOT NULL;
ALTER TABLE contacts ALTER COLUMN name DROP NOT NULL;
ALTER TABLE contacts RENAME COLUMN phone TO mobile;
ALTER TABLE contacts ADD COLUMN notes TEXT;
sqlite3 -table contacts.db ".mode table --titlelimit 0" ".read other_alters.sql"
+---------------------------------------------------------------------+
| raise(ABORT,'Cannot add a NOT NULL column with default value NULL') |
+---------------------------------------------------------------------+
+--------------------------------------+
| sqlite_fail('constraint failed', 19) |
+--------------------------------------+
Two of them printed a header: adding a NOT NULL column, which SQLite allows only on an empty table, and SET NOT NULL, which needs every row to have a value. Both check the table's rows first. DROP NOT NULL, RENAME COLUMN and a plain ADD COLUMN check nothing and printed nothing.
The SQLite Forum. SQLite takes bug reports on the SQLite Forum. I searched it for sqlite_fail, ADD CONSTRAINT and SET NOT NULL, and found no report of this.
Why It Happens
To make sure a change is safe, these ALTER TABLE commands run internal SELECT queries that fail on purpose when something is wrong: a constraint with the same name, or a row that breaks the new rule. When nothing is wrong, the queries find no rows and the change goes through. But the statement still reports a result column, titled with the text of the check.
Until SQLite 3.54.0, the shell printed nothing for a result with no rows. Since 3.54.0, its columnar modes, such as table, print the header anyway, so the check's title shows up as an empty table. It's harmless: the change worked.
The Fix
The statements worked, so there's nothing to undo or run again. To stop the shell printing these headers, switch it back to how it showed column titles before 3.54.0: only above rows. The release notes give the setting: "Legacy behavior can be restored using "-titles on"."
Delete helpdesk.db, so the script can create the table again, and run:
sqlite3 -table helpdesk.db ".mode table --titles on" ".read replace_check.sql"
- The shell runs each quoted argument after the database file in turn, so
.moderuns before.read. .mode tablekeeps the table output, and--titles onprints column titles only when there are rows.
The command is the same in bash, zsh and PowerShell. At the sqlite> prompt, type .mode table --titles on before the ALTER TABLE. In a script, make it the first line.
After the Fix
+-------------+----------+
| title | priority |
+-------------+----------+
| Printer jam | low |
| Server down | urgent |
+-------------+----------+

Only the SELECT's table came back, as I'd expected the first time. One thing changes with --titles on: a SELECT that matches no rows prints nothing at all, which is how the shell behaved before 3.54.0.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Running the ALTER TABLE ... ADD CONSTRAINT again, because the header looks like an error | constraint valid_priority already exists: the first run had worked | Check the table with .schema tickets first |
Putting .mode table --titles on after .read | The script runs before the setting changes, and the header still appears | Put the .mode command first |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
An empty table headed sqlite_fail('cons... after ALTER TABLE ... ADD CONSTRAINT | The 3.54 shell prints the title of SQLite's internal check, which found nothing wrong | Nothing to fix. Use .mode table --titles on to hide it |
An empty table headed raise(ABORT,'Cann... after ALTER TABLE ... ADD COLUMN ... NOT NULL | The same, for the check that the table is empty | The same |
An empty table headed sqlite_fail('cons... after ALTER COLUMN ... SET NOT NULL | The same, for the check that no row holds NULL | The same |
Parse error near line 1 of replace_check.sql: table tickets already exists | The script already ran on helpdesk.db | Delete helpdesk.db and run the script again |
FAQ
Did the ALTER TABLE fail?
No. A failed statement prints an Error or Parse error line and changes nothing. Here .schema showed the new rule, and the new list let urgent in.
Which versions show it?
The 3.54 shell. ADD CONSTRAINT, DROP CONSTRAINT and SET NOT NULL came in SQLite 3.53.0, and the header for results with no rows came in 3.54.0. I saw it on 3.54.1, the newest release when I wrote this.
Does it happen without -table?
Yes, in a terminal. Started there without a mode option, the 3.54.1 shell reported its mode as qbox --limits on --quote relaxed --sw auto --textjsonb on, a table drawn with box lines, and it printed the same headers. .mode --titles on, with no mode name, changes only the titles and keeps that mode. When the shell's output goes to a file or another program, it uses list mode instead, which prints no column titles, and these statements printed nothing.
Conclusion
The empty sqlite_fail('cons... table after ALTER TABLE ... ADD CONSTRAINT isn't an error: the constraint was replaced. It's the title of SQLite's internal check, which the 3.54 shell prints even when no rows come back, and .mode table --titles on hides it.
Additional Resources
- SQLite 3.54.0 release notes — the header change and the
--titlessetting. - ALTER TABLE — the syntax of
ADD CONSTRAINT,DROP CONSTRAINTandALTER COLUMN. - Command Line Shell For SQLite — the
.modecommand and the shell's options.
