ankane/blind_index

'Not null' constraint prevents record creation during migration

david-mears-2 opened this issue · 1 comments

I hope I'm following the documentation correctly. Using ActiveRecord, I have:

  • Added the new email_ciphertext column
  • Backfilled the data
  • Updated the model:
class Feedback
  encrypts :email
  self.ignored_columns = ["email"]
end

In my schema, prior to using lockbox, there is a 'not null' constraint on email:

t.string "email", null: false

Thus, once I add self.ignored_columns = ["email"] and try to create an instance of feedback, I will be violating this constraint by only filling the email_ciphertext column:

pry(main)> Feedback.create(email: "david.mears@digital.example.gov.uk")
  TRANSACTION (0.2ms)  BEGIN
  Feedback Create (0.8ms)  INSERT INTO "feedbacks" ("created_at", "updated_at", "email_ciphertext") VALUES ($1, $2, $3) RETURNING "id"  [["created_at", "2021-07-21 13:13:39.427761"], ["updated_at", "2021-07-21 13:13:39.427761"], ["email_ciphertext", "oCGZnkq7X3EnXX6nq8oAUJCeE7F1VIWrQbMiv1Ipy7ZorLu3xOWYDd5OQcuHMxlD09A/wDixca60Z2Q2gZ2QHA=="]]
  TRANSACTION (0.2ms)  ROLLBACK
ActiveRecord::NotNullViolation: PG::NotNullViolation: ERROR:  null value in column "email" of relation "feedbacks" violates not-null constraint
DETAIL:  Failing row contains (4acf9be5-5367-4131-996e-7118e4dd9123, 2021-07-21 13:13:39.427761, 2021-07-21 13:13:39.427761, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, f, null, null, oCGZnkq7X3EnXX6nq8oAUJCeE7F1VIWrQbMiv1Ipy7ZorLu3xOWYDd5OQcuHMxlD..., null).

from /Users/daivdmears/.asdf/installs/ruby/3.0.2/lib/ruby/gems/3.0.0/gems/activerecord-6.1.4/lib/active_record/connection_adapters/postgresql_adapter.rb:672:in `exec_params'
Caused by PG::NotNullViolation: ERROR:  null value in column "email" of relation "feedbacks" violates not-null constraint
DETAIL:  Failing row contains (4acf9be5-5367-4131-996e-7118e4dd9123, 2021-07-21 13:13:39.427761, 2021-07-21 13:13:39.427761, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, f, null, null, oCGZnkq7X3EnXX6nq8oAUJCeE7F1VIWrQbMiv1Ipy7ZorLu3xOWYDd5OQcuHMxlD..., null).

from /Users/daivdmears/.asdf/installs/ruby/3.0.2/lib/ruby/gems/3.0.0/gems/activerecord-6.1.4/lib/active_record/connection_adapters/postgresql_adapter.rb:672:in `exec_params'

What is the purpose of ignored_columns =?

Forgive me, this was meant to be on the lockbox repo, not this one. Will copy.