lundi 15 février 2016

Running the test command gives a postgresql uniqueness error

I switched my database from sqlite to postgresql recently. The database.yml file is given below.

default: &default
      adapter: postgresql
      encoding: unicode
      pool: 5
      username: ubuntu
      password: password
      host: <%= ENV['IP'] %>


development:
  <<: *default
  database: app_development

test:
  <<: *default
  database: app_test

production:
  <<: *default
  database: app_production

The list of all the databases is also given below.

  Name       |  Owner   | Encoding  | Collate | Ctype |   Access privileges   
-----------------+----------+-----------+---------+-------+-----------------------
 app_development | postgres | SQL_ASCII | C       | C     | 
 app_production  | ubuntu   | SQL_ASCII | C       | C     | 
 app_test        | ubuntu   | UTF8      | C       | C     | 
 postgres        | postgres | SQL_ASCII | C       | C     | 
 score_app       | postgres | SQL_ASCII | C       | C     | 
 template0       | postgres | SQL_ASCII | C       | C     | =c/postgres          +
                 |          |           |         |       | postgres=CTc/postgres
 template1       | ubuntu   | UTF8      | C       | C     | 

It seems to work perfectly fine in development. The problem occurs when I run the bundle exec rake test command. The command works but my test gives me 50 errors. The errors are regarding the same point. The error is given below.

ActiveRecord::RecordNotUnique: PG::UniqueViolation: ERROR:  duplicate key value violates unique constraint "index_friendships_on_user_id_and_friend_id"
DETAIL:  Key (user_id, friend_id)=(0, 0) already exists.
: INSERT INTO "friendships" ("user_id", "friend_id", "approved", "created_at", "updated_at", "id") VALUES (0, 0, 'f', '2016-02-15 16:49:26', '2016-02-15 16:49:26', 298486374) 

I have a friendship model where users have to be friends in order for them to access the conversation feature. The users can't friend themselves therefore, user_id and friend_id must be unique. I understand that. However, I haven't written any tests for that model yet.

I am not sure why I am getting an error associated with this. I am not sure if I did the configuration wrong or maybe postgresql is different that sqlite when it comes to running tests. I have tried to include all the relevant code. I wasn't sure what code to include. As always, any help would be greatly appreciated.

Aucun commentaire:

Enregistrer un commentaire