Skip to main content

Fixed: "sqlite_fail('constraint valid_priority already exists', 1)" After ALTER TABLE ADD CONSTRAINT in SQLite

· 10 min read
Jagdish Kumawat
Founder @ Dewiride

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:

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;

The replace_check.sql script in a macOS-style code window: a tickets table whose priority column has a check named valid_priority allowing low, medium and high, an insert, ALTER TABLE statements that drop valid_priority and add it again with urgent in the list, an insert with the priority urgent, and a SELECT of every row.

  • 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, with urgent in it.
  • The second INSERT uses urgent, which only the new list allows.

I ran it with the sqlite3 shell from SQLite 3.54.1:

Terminal
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​

Output
+----------------------+
| sqlite_fail('cons... |
+----------------------+
+-------------+----------+
| title | priority |
+-------------+----------+
| Printer jam | low |
| Server down | urgent |
+-------------+----------+

The output in a terminal: first an empty table with a single column headed sqlite_fail('cons..., then the tickets table with Printer jam at low and Server down at 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:

Terminal
sqlite3 helpdesk.db ".schema tickets"
Output
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:

Terminal
sqlite3 helpdesk.db "INSERT INTO tickets (title, priority) VALUES ('Coffee machine', 'someday');"
Output
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:

Terminal
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'));"
Output
+------------------------------------------------------------+
| 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:

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;
Terminal
sqlite3 -table contacts.db ".mode table --titlelimit 0" ".read other_alters.sql"
Output
+---------------------------------------------------------------------+
| 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:

Terminal
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 .mode runs before .read.
  • .mode table keeps the table output, and --titles on prints 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​

Output
+-------------+----------+
| title | priority |
+-------------+----------+
| Printer jam | low |
| Server down | urgent |
+-------------+----------+

The output in a terminal after running with .mode table --titles on: only the tickets table, with Printer jam at low and Server down at urgent, and no empty sqlite_fail table above it.

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​

MistakeWhat happensDo this instead
Running the ALTER TABLE ... ADD CONSTRAINT again, because the header looks like an errorconstraint valid_priority already exists: the first run had workedCheck the table with .schema tickets first
Putting .mode table --titles on after .readThe script runs before the setting changes, and the header still appearsPut the .mode command first

Troubleshooting​

SymptomLikely causeFix
An empty table headed sqlite_fail('cons... after ALTER TABLE ... ADD CONSTRAINTThe 3.54 shell prints the title of SQLite's internal check, which found nothing wrongNothing to fix. Use .mode table --titles on to hide it
An empty table headed raise(ABORT,'Cann... after ALTER TABLE ... ADD COLUMN ... NOT NULLThe same, for the check that the table is emptyThe same
An empty table headed sqlite_fail('cons... after ALTER COLUMN ... SET NOT NULLThe same, for the check that no row holds NULLThe same
Parse error near line 1 of replace_check.sql: table tickets already existsThe script already ran on helpdesk.dbDelete 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​