GRDB SQLite problem

Hi guys! I am new one here.

I am getting this error "SQLite error 19: NOT NULL constraint failed:".

[">>> SQLite error 19: NOT NULL constraint failed: tableName.name - while executing `INSERT INTO \"tableName\" (\"id\", \"first_name\", \"second_name\") VALUES (?,?,?)` with arguments [3, NULL, NULL]"]

I tried to remove/ drop colum but it's impossible because I don't have option to do like this:

db.alter(table: "tableName") { t in
    t.drop(column: "columnName", .text)
} 

I really have no idea what to do.

Hello @Eldar,

Which is your problem (pick one):

  1. That the database table contains a NOT NULL constraint on the name column, when you do want to insert nulls in the database?
  2. That you end up inserting nulls in the database, when you know that missing names are something that should never happen in your application?

Hello @gwendal.roue and thank you for answering!

The main problem is next one:

Database column is not null, it contains string value, but when I remove .notNull from column I got this error:

GRDB/FetchableRecord+Decodable.swift:7: Fatal error: 'try!' expression unexpectedly raised an error: could not decode String from database value NULL - column: "firstName", column index: 1, row: [id:1 firstName:NULL secondName:NULL working:0 completed:2 missed:1], sql: `Select tableName.id, tableName.fistName, tableName.secondName etc. 

But using .notNull() I am receiving the first error message as I mentinoed before. Hope so I will find a way to fix this.

Thank you sir again!

Database column is not null, it contains string value.

Well, this is not what the error you paste below says. It says that the database contains a NULL in the firstName column.

Could not decode String from database value NULL - column: "firstName", column index: 1, row: [id:1 firstName:NULL secondName:NULL working:0 completed:2 missed:1]

I wish I could help you, but I can't, considering the confusing information you provide. Please try again to answer my initial question, please.

In other words: make up your mind. Do you want NULL to go in the database, or not? When this has been settled, we'll see what you need in order to make it happen without any error.

1 Like
Terms of Service

Privacy Policy

Cookie Policy