SQLite Forum

Removing not null constraint doesn't behave correctly
Login
Hi all,

I am wondering if I am seeing a SQLite bug or not.

These are the SQL commands I'm running.

```
BEGIN TRANSACTION;

/* Create a table called NAMES */
CREATE TABLE NAMES(Id integer PRIMARY KEY, Name text NOT NULL);

/* Create few records in this table */
INSERT INTO NAMES VALUES(1,'Tom');
INSERT INTO NAMES VALUES(2,'Lucy');
INSERT INTO NAMES VALUES(3,'Frank');
INSERT INTO NAMES VALUES(4,'Jane');
INSERT INTO NAMES VALUES(5,'Robert');
COMMIT;

SELECT * FROM sqlite_master;

/* Remove the not null constraint on Name, using the "simpler procedure" outlined in Section 5 of https://www.sqlite.org/lang_altertable.html */
BEGIN TRANSACTION;
PRAGMA writable_schema = on;
UPDATE sqlite_master SET sql = 'CREATE TABLE NAMES(Id integer PRIMARY KEY, Name text)' WHERE type = 'table' AND name = 'NAMES';
PRAGMA schema_version = 2;
PRAGMA writable_schema = off;
COMMIT;
-- This prints "ok".
PRAGMA integrity_check;

SELECT * FROM sqlite_master;

PRAGMA table_info(sqlite_master);

/* Try to add another column. */
BEGIN TRANSACTION;
ALTER TABLE NAMES ADD Age INTEGER DEFAULT 10;
COMMIT;

/* Display all the records from the table */
SELECT * FROM NAMES;
```

Adding the "Age" column results in "Error: near line 29: malformed database schema (NAMES) - near ",": syntax error".

I noticed the following behaviour:

* If I don't remove the NOT NULL constraint on Name, no error is exhibited.
* If I keep the NOT NULL constraint and add a default value to Name (which is documented as supported), the schema appears to update correctly but the subsequently added new column Age is now populated with the default value of Name, instead of its own default value.
* If I keep the NOT NULL constraint and change the default value of Name (which is also documented as supported), the schema again appears to update correctly but the following migration to add Age fails with `Error: near line 29: malformed database schema (NAMES) - near ",": syntax error`.