Sto seguendo il tutorial sui binari. Sono su ch 6 e sto ricevendo uno strano errore con SQLite3 (per la cronaca, sto usando sqlite versione 1.3.10 e il tutorial usa 1.3.9)Esercitazione guide: SQLite3 :: ConstraintException: vincolo UNIQUE non riuscito: users.email
Non ricevo un errore quando eseguo rake db: migrare, ma quando ho eseguito la migrazione per l'ambiente di test, ecco cosa ottengo:
$ bundle exec rake test:models
rake aborted!
ActiveRecord::PendingMigrationError:
Migrations are pending. To resolve this issue, run:
bin/rake db:migrate RAILS_ENV=test
sample_app/test/test_helper.rb:3:in `<top (required)>'
sample_app/test/models/user_test.rb:1:in `require'
sample_app/test/models/user_test.rb:1:in `<top (required)>'
Tasks: TOP => test:models
(See full trace by running task with --trace)
$ bundle exec rake db:migrate RAILS_ENV=test
== 20150628011937 AddIndexToUsersEmail: migrating ===========================
==
-- add_index(:users, :email, {:unique=>true})
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:
SQLite3::ConstraintException: UNIQUE constraint failed: users.email: CREATE UNIQUE INDEX "index_users_on_email" ON "users" ("email")sample_app/db/migrate/20150628011937_add_index_to_users_email.rb:3:in `change'
C:in `migrate'
ActiveRecord::RecordNotUnique: SQLite3::ConstraintException: UNIQUE constrain
t failed: users.email: CREATE UNIQUE INDEX "index_users_on_email" ON "users"
("email")
sample_app/db/migrate/20150628011937_add_index_to_users_email.rb:3:in `change'
C:in `migrate'
SQLite3::ConstraintException: UNIQUE constraint failed: users.email
sample_app/db/migrate/20150628011937_add_index_to_users_email.rb:3:in `change'
C:in `migrate'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)
Ecco il mio modello user.rb:
class User < ActiveRecord::Base
before_save { self.email = email.downcase }
validates :name, presence: true, length: { maximum: 50 }
VALID_EMAIL_REGEX = /\A[\w+\-.][email protected][a-z\d\-.]+\.[a-z]+\z/i
validates :email, presence: true, length: { maximum: 255 },
format: { with: VALID_EMAIL_REGEX },
uniqueness: { case_sensitive: false }
end
qui è la mia più recente migrazione
class AddIndexToUsersEmail < ActiveRecord::Migration
def change
add_index :users, :email, unique: true
end
end
Posso postare qualsiasi altro file se questi sono rilevanti. Grazie in anticipo per l'aiuto!
Controlla la tua/infissi file 'test/users.yml' e assicurarsi' email' è unico per ogni utente. –
Il tutorial dice di sostituire tutti i contenuti di quel file solo il commento "# vuoto". Dovrei aggiungere qualcos'altro? –
Va bene. Il tutorial usa 'users.yml' solo dal capitolo 8 in poi, e vuole che sia vuoto in questa fase. Esegui 'bundle exec rake db: migrate' per migrare il database dev. 'Se fallisce, prova ad uscire da qualsiasi sessione di console sandbox in esecuzione, che può bloccare il database e impedire le migrazioni.' ' –