Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

no such function: NOW during migration

In Ruby on Rails I'm creating a devise confirmable for users migration, but when I migrate I get error:

StandardError: An error has occurred, this and all later migrations canceled

SQLite3::SQLException: no such function: NOW: UPDATE users SET confirmed_at = NOW()

Migration:

class AddConfirmableToDevise < ActiveRecord::Migration[5.0]
  def up
    add_column :users, :confirmation_token, :string
    add_column :users, :confirmed_at, :datetime
    add_column :users, :confirmation_sent_at, :datetime
    # add_column :users, :unconfirmed_email, :string # Only if using reconfirmable
    add_index :users, :confirmation_token, unique: true
    # User.reset_column_information # Need for some types of updates, but not for update_all.
    # To avoid a short time window between running the migration and updating all existing
    # users as confirmed, do the following
    execute("UPDATE users SET confirmed_at = NOW()")
    # All existing user accounts should be able to log in after this.
    # Remind: Rails using SQLite as default. And SQLite has no such function :NOW.
    # Use :date('now') instead of :NOW when using SQLite.
    # => execute("UPDATE users SET confirmed_at = date('now')")
    # Or => User.all.update_all confirmed_at: Time.now
  end

  def down
    remove_columns :users, :confirmation_token, :confirmed_at, :confirmation_sent_at
    # remove_columns :users, :unconfirmed_email # Only if using reconfirmable
  end
end
like image 529
Kristis Avatar asked Jul 13 '26 01:07

Kristis


1 Answers

To quote your question:

# Use :date('now') instead of :NOW when using SQLite.
# => execute("UPDATE users SET confirmed_at = date('now')")
like image 165
CL. Avatar answered Jul 14 '26 16:07

CL.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!