Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue with id when creating a new object using MySql #13052

Closed
kuldeepaggarwal opened this issue Nov 26, 2013 · 2 comments
Closed

Issue with id when creating a new object using MySql #13052

kuldeepaggarwal opened this issue Nov 26, 2013 · 2 comments

Comments

@kuldeepaggarwal
Copy link
Contributor

class User < ActiveRecord::Base
end

when I am creating an user, like

>> u = User.create(id: 'test', name: 'KD')
SQL (16.9ms)  INSERT INTO `users` (`created_at`, `id`, `name`, `updated_at`) VALUES ('2013-11-26 12:57:51', 0, '\nKD', '2013-11-26 12:57:51')
=> #<User id: 0, name: "KD", created_at: "2013-11-26 12:57:51", updated_at: "2013-11-26 12:57:51">

It is initializing the object with id = 0(because id was set to 'test'), but actual id stored in DB is '1'.

>> User.last
=> #<User id: 1, name: "KD", created_at: "2013-11-26 12:57:51", updated_at: "2013-11-26 12:57:51">

So is this correct or a bug? Shouldn't the id be last generated id, i.e. 1

@dmathieu
Copy link
Contributor

You shouldn't define primary keys yourself. Just let your database define it.
Also, most database engines are optimized for integer primary keys. You shouldn't use strings.

If what you're looking to do is add slugs to models, take a look at friendly_id.

@arunagw
Copy link
Member

arunagw commented Nov 26, 2013

True. Why you want to assign a primary_key which is auto incremented anyway

Closing this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants