jeudi 23 juillet 2015

sqlite insert or ignore always inserts on empty field

I am getting multiple inserts when a field is empty despite using insert or ignore with a primary key. I also tried adding a unique index, just to be sure. I only want new inserts when the primary key is not equal to any existing row.

According to http://ift.tt/1IfUxrr nulls are distinct in a UNIQUE column these duplicate inserts should not happen.

I'm doing this insert:

INSERT OR IGNORE INTO facts (
  created,
  inserted,
  author,
  kind,
  what,
  field,
  val
  ) VALUES (?, ?, ?, ?, ?, ?, ?)

with this table:

CREATE TABLE IF NOT EXISTS facts (
  created TEXT,
  inserted TEXT,
  author TEXT,
  kind TEXT,
  what TEXT,
  field TEXT,
  val TEXT,
  PRIMARY KEY(created, author, kind, what, field, val)
  )

and getting this result after doing the same inserts multiple times:

sqlite> select * from facts where what='vot1dafjt95326qvs8kn' order by inserted;
2014-01-29T09:30:44.000000|2015-07-23T13:23:10.643060|test_user_id|TaskFact|vot1dafjt95326qvs8kn|created|2014-01-29T09:30:44.000000
2014-01-29T09:30:44.000000|2015-07-23T13:23:10.643152|test_user_id|TaskFact|vot1dafjt95326qvs8kn|alarm|
2014-01-29T09:30:44.000000|2015-07-23T13:23:10.643315|test_user_id|TaskFact|vot1dafjt95326qvs8kn|deleted|
2014-01-29T09:30:44.000000|2015-07-23T13:23:10.643380|test_user_id|TaskFact|vot1dafjt95326qvs8kn|description|
2014-01-29T09:30:44.000000|2015-07-23T13:23:10.643445|test_user_id|TaskFact|vot1dafjt95326qvs8kn|location|KG45
2014-01-29T09:30:44.000000|2015-07-23T13:23:10.643641|test_user_id|TaskFact|vot1dafjt95326qvs8kn|summary|semtid
2014-01-29T09:30:44.000000|2015-07-23T13:23:10.643780|test_user_id|TaskFact|vot1dafjt95326qvs8kn|when|TS('2014-03-14T07:00:00.000000', '2014-03-14T11:00:00.000000')
2014-01-29T09:30:44.000000|2015-07-23T13:23:35.559110|test_user_id|TaskFact|vot1dafjt95326qvs8kn|alarm|
2014-01-29T09:30:44.000000|2015-07-23T13:23:35.559273|test_user_id|TaskFact|vot1dafjt95326qvs8kn|deleted|
2014-01-29T09:30:44.000000|2015-07-23T13:24:12.548969|test_user_id|TaskFact|vot1dafjt95326qvs8kn|alarm|
2014-01-29T09:30:44.000000|2015-07-23T13:24:12.549186|test_user_id|TaskFact|vot1dafjt95326qvs8kn|deleted|
2014-01-29T09:30:44.000000|2015-07-23T13:25:39.638743|test_user_id|TaskFact|vot1dafjt95326qvs8kn|alarm|
2014-01-29T09:30:44.000000|2015-07-23T13:25:39.638906|test_user_id|TaskFact|vot1dafjt95326qvs8kn|deleted|

The deleted and alarm rows are inserted multiple times, but the other rows are only inserted once.

I am doing the inserts from python3 using the sqlite3 module.

Aucun commentaire:

Enregistrer un commentaire