fightstarr20
fightstarr20

Reputation: 12598

SQLite - NOT NULL constraint failed

I am trying to create a simple SQLite database that will allow me to store email addresses and timestamps. I have created the table like this:

$sql =<<<EOF
        CREATE TABLE ENTRIES
        (ID INT PRIMARY KEY      NOT NULL,
        EMAIL           EMAIL    NOT NULL,
        TIMESTAMP DATETIME DEFAULT CURRENT_TIMESTAMP);
EOF;

And I am trying to insert an email like this:

$sql =<<<EOF
      INSERT INTO ENTRIES (EMAIL)
      VALUES ([email protected]);
EOF;

I am getting an error

NOT NULL constraint failed: ENTRIES.ID

I am assuming this is to do with the ID and autoincrement? I have read the docs and it advises against using autoincrement. Where am I going wrong?

Upvotes: 6

Views: 35872

Answers (1)

user743382
user743382

Reputation:

The docs say:

If a table contains a column of type INTEGER PRIMARY KEY, then that column becomes an alias for the ROWID.

And because it becomes an alias for the ROWID, it's not necessary to explicitly specify a value.

You have INT PRIMARY KEY, not INTEGER PRIMARY KEY. If you change it to INTEGER PRIMARY KEY, it works the way you expect.

Upvotes: 25

Related Questions