nullobject/rein

add_numericality_constraint fails with operator does not exist: integer

chrishough opened this issue · 1 comments

This is the migration I am trying to use...

class CreateContacts < ActiveRecord::Migration[6.0]
  def change
    create_table :contacts do |t|
      t.integer :phone_number, null: :false, length: { minimum: 10, maximum: 10 }

      t.timestamps
    end

    add_numericality_constraint :contacts, :phone_number
    add_match_constraint :contacts, :phone_number, accepts: '[1-9]{1}[\d]{9}'
  end
end

This is the PG error that comes back...

Caused by:
PG::UndefinedFunction: ERROR:  operator does not exist: integer ~ unknown
HINT:  No operator matches the given name and argument types. You might need to add explicit type casts.
...db/migrate/20200330011140_create_contacts.rb:10:in `change'
...rbenv/versions/2.7.0/bin/bundle:23:in `load'
...rbenv/versions/2.7.0/bin/bundle:23:in `<main>'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)

Does anyone have thoughts on this?

I ended up using a string for this instead of a bigint...

  def change
    create_table :contacts do |t|
      t.string :phone_number, null: false

      t.timestamps
    end

    add_match_constraint :contacts, :phone_number, accepts: '[1-9]{1}[\d]{9}'
    add_length_constraint :contacts, :phone_number, equal_to: 10
  end