Test Test
Test Test

Reputation: 2889

Data not entering sqlite database - Ruby on Rails

I am running "rails console" and then the following command:

 User.create(name:"John", email:"[email protected]", password:"foo", password_confirmation:"foo")

and i get this:

   (0.1ms)  begin transaction
  User Exists (0.2ms)  SELECT 1 FROM "users" WHERE LOWER("users"."email") = LOWER('[email protected]') LIMIT 1
   (0.1ms)  rollback transaction
 => #<User id: nil, name: "John", email: "[email protected]", created_at: nil, updated_at: nil, password_digest: "$2a$10$mY0/9RgjwOU46ZYcSC0TFOCMxrPiqWTEHWe1K27O/3Ya...">

when i check the file of the sqlite database using SQLite Database browser I see nothing.

here is my user model:

class User < ActiveRecord::Base

    #these attributes can be modified by the users
    attr_accessible :name, :email, :password, :password_confirmation
    #ruby's way of calling a method below...
    has_secure_password

    #validation testing
    validates :name, presence: true, length: { maximum: 50 }
    #regular expression (there is an official one)
    VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
    #and add it..
    validates :email, presence: true, format: { with: VALID_EMAIL_REGEX },
        uniqueness:  { case_sensitive: false }
    #validate password 
    validates :password, length: {minimum: 6}
    validates :password_confirmation, presence: true

end

why is data not entered in my database?

I get this error with whatever I enter!

This for example:

1.9.3p125 :005 > User.create(name:"Smith", email:"[email protected]", password:"foo", password_confirmation:"foo")
   (0.1ms)  begin transaction
  User Exists (0.1ms)  SELECT 1 FROM "users" WHERE LOWER("users"."email") = LOWER('[email protected]') LIMIT 1
   (0.0ms)  rollback transaction
 => #<User id: nil, name: "Smith", email: "[email protected]", created_at: nil, updated_at: nil, password_digest: "$2a$10$6nzyRJ0IplI6B4bSoQEtUOIcrbFVl1ix3EAKPGJZjZQf...">

I never entered a Smith user with that email, and I still get that "User Exists"!

EDIT:

I got the error. The password limit is 5 I was entering a 3-letter password so when I type this:

User.create(name:"Smith", email:"[email protected]", password:"foobar", password_confirmation:"foobar")
   (0.1ms)  begin transaction
  User Exists (0.2ms)  SELECT 1 FROM "users" WHERE LOWER("users"."email") = LOWER('[email protected]') LIMIT 1
Binary data inserted for `string` type on column `password_digest`
  SQL (1.7ms)  INSERT INTO "users" ("created_at", "email", "name", "password_digest", "updated_at") VALUES (?, ?, ?, ?, ?)  [["created_at", Mon, 12 Mar 2012 00:16:42 UTC +00:00], ["email", "[email protected]"], ["name", "Smith"], ["password_digest", "$2a$10$v/FqAuUPpbdIJ44jVHxbKOJt/uoBTJVkP4KIhzJHNcF8rWPFfKusi"], ["updated_at", Mon, 12 Mar 2012 00:16:42 UTC +00:00]]
   (266.9ms)  commit transaction
 => #<User id: 1, name: "Smith", email: "[email protected]", created_at: "2012-03-12 00:16:42", updated_at: "2012-03-12 00:16:42", password_digest: "$2a$10$v/FqAuUPpbdIJ44jVHxbKOJt/uoBTJVkP4KIhzJHNcF8...">

I works, but i still get this weird User Exists error... Any ideas?

Upvotes: 7

Views: 4912

Answers (8)

SteveLin
SteveLin

Reputation: 72

In fact, the problem is not "User Exists", and the real reason is that the password is too short. You can show the errors by inputing the user.errors on the rails console.

please refer to the following error example.

irb(main):013:0> user = User.new(name:"caiqinghua", email:"[email protected]", password:"admin",password_confirmation:"admin")

=> #<User id: nil, name: "caiqinghua", email: "[email protected]", created_at: nil, updated_at: nil, password_digest: "$2a$10$1d5jtpdkpl9hJPr/8s/dku1Y34.Aft/cAP5h/wrTN2sL...">
irb(main):014:0> user.save
   (0.2ms)  begin transaction
  User Exists (0.3ms)  SELECT 1 AS one FROM "users" WHERE LOWER("users"."email") = LOWER('[email protected]') LIMIT 1
   (0.2ms)  rollback transaction
=> false
irb(main):015:0> User.all
  User Load (0.6ms)  SELECT "users".* FROM "users"
=> #<ActiveRecord::Relation []>
irb(main):016:0> User.create(name:"caiqing", email:"[email protected]", password:"admin",password_confirmation:"admin")
   (0.3ms)  begin transaction
  User Exists (0.3ms)  SELECT 1 AS one FROM "users" WHERE LOWER("users"."email") = LOWER('[email protected]') LIMIT 1
   (0.1ms)  rollback transaction
=> #<User id: nil, name: "caiqing", email: "[email protected]", created_at: nil, updated_at: nil, password_digest: "$2a$10$Ossfc7NsL6/MjYVEjT5rJe/y4AiqdNZI2tCkrN1h8rHx...">

**irb(main):017:0> user.errors**
=> #<ActiveModel::Errors:0xba7d34c0 @base=#<User id: nil, name: "caiqinghua", email: "[email protected]", created_at: nil, updated_at: nil, password_digest: "$2a$10$1d5jtpdkpl9hJPr/8s/dku1Y34.Aft/cAP5h/wrTN2sL...">, @messages={:password=>["is too short (minimum is 6 characters)"]}>
irb(main):018:0> 

If i changed the password from "admin" to "admin123", and nothing is wrong.

irb(main):018:0> 
irb(main):019:0* user = User.new(name:"caiqinghua", email:"[email protected]", password:"admin123",password_confirmation:"admin123")
=> #<User id: nil, name: "caiqinghua", email: "[email protected]", created_at: nil, updated_at: nil, password_digest: "$2a$10$dHb.jezaiomN.ZE0pazkOOHDdac/K386h7zsORF93HtQ...">
irb(main):020:0> user.save
   (33.7ms)  begin transaction
  User Exists (9.9ms)  SELECT 1 AS one FROM "users" WHERE LOWER("users"."email") = LOWER('[email protected]') LIMIT 1
Binary data inserted for `string` type on column `password_digest`
  SQL (108.8ms)  INSERT INTO "users" ("created_at", "email", "name", "password_digest", "updated_at") VALUES (?, ?, ?, ?, ?)  [["created_at", Fri, 27 Sep 2013 15:25:27 UTC +00:00], ["email", "[email protected]"], ["name", "caiqinghua"], ["password_digest", "$2a$10$dHb.jezaiomN.ZE0pazkOOHDdac/K386h7zsORF93HtQb7wtj73Ha"], ["updated_at", Fri, 27 Sep 2013 15:25:27 UTC +00:00]]
   (112.7ms)  commit transaction
=> true
irb(main):021:0> User.all
  User Load (0.6ms)  SELECT "users".* FROM "users"
=> #<ActiveRecord::Relation [#<User id: 2, name: "caiqinghua", email: "[email protected]", created_at: "2013-09-27 15:25:27", updated_at: "2013-09-27 15:25:27", password_digest: "$2a$10$dHb.jezaiomN.ZE0pazkOOHDdac/K386h7zsORF93HtQ...">]>
irb(main):022:0> 

Upvotes: 2

ocodo
ocodo

Reputation: 30248

  1. user = User.new

  2. set the fields on user

  3. user.save

  4. look at user.errors

Upvotes: 8

user2008535
user2008535

Reputation: 11

It's the password.

If you want to check the errors do this:

user = User.new(name:"John", email:"[email protected]", password:"foo", 
password_confirmation:"foo")

user.save

user.errors

Upvotes: 1

Arthur Camara
Arthur Camara

Reputation: 577

Just got here using a Google Search and found what the issue is. In fact, it is a rubbish error message. Even with a clean database it would appear. The problem is: the password is only 3 characters long, wich will cause an issue with

    validates :password, length: {minimum: 6}

So, if you try with a longer password (and confirmation) it should work. (PS: I'm using MySQL server, not SQLite, but i'm pretty sure the error is the same)

Upvotes: 8

regrecall
regrecall

Reputation: 531

I encountered this problem too, because of my password is not longer than the 6. So I changed it to the longer one, it worked. But I didn't know why this exception named "User Existed", or
Something wrong with me. BYW, I didn't open the SQLIte Database Browser.

Upvotes: 1

Ryan W Kan
Ryan W Kan

Reputation: 380

I'm not 100% sure but I think

User Exists (0.4ms) SELECT 1 FROM "users" WHERE LOWER("users"."email") = LOWER('[email protected]') LIMIT 1

merely checks for uniqueness in that it will run regardless of whether your query will pass or fail.

If you pay a closer attention to the results, you will realize that a successful outcome will display 'User Exists' in blue, while a failed outcome when you actually try to insert a duplicate user will display 'User Exists' in red (or purple in my case).

Ryan

Upvotes: 1

erogol
erogol

Reputation: 13614

Maybe you are looking to the wrong database. This is rails app. and look to the balbla.development schema in database since there is no way about your problem that there must be a row that has same email value

Upvotes: 1

Ben Lee
Ben Lee

Reputation: 53319

You are attempting to create a duplicate row in the database where a unique validation exists. See your error message ("User Exists"):

User Exists (0.2ms)  SELECT 1 FROM "users" WHERE LOWER("users"."email") = LOWER('[email protected]') LIMIT 1

See the line in your model:

validates :email, presence: true, format: { with: VALID_EMAIL_REGEX },
    uniqueness:  { case_sensitive: false }

It's validating that the user's email address is unique. So you must already have a row in the database with "[email protected]" as the email address.

Upvotes: 1

Related Questions