After deploying my application I created a new user and noticed that the address bar was reporting number 2 at the end of the url: http://...herokuapp.com/users/2.
So I checked with rails console in production environment first and
then at Heroku. There seemed to be nothing irregular in my production
environment (running sqlite3),
With heroku run console
I got the following results:
irb(main):001:0> User.count
(4.9ms) SELECT COUNT(*) FROM "users"
(4.9ms) SELECT COUNT(*) FROM "users"
=> 1
irb(main):002:0> User.first
User Load (2.5ms) SELECT "users".* FROM "users" ORDER BY "users"."id" ASC LIMIT 1
User Load (2.5ms) SELECT "users".* FROM "users" ORDER BY "users"."id" ASC LIMIT 1
=> #<User id: 2, name: "krfg", email: "kr...@example.com", created_at: "2015-12-20 13:56:04", updated_at: "2015-12-20 13:56:04", password_digest: "...">
irb(main):001:0> User.find(1)
User Load (1.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 1]]
User Load (1.1ms) SELECT "users".* FROM "users" WHERE "users"."id" = $1 LIMIT 1 [["id", 1]]
ActiveRecord::RecordNotFound: Couldn't find User with 'id'=1
Did you create a user and then delete it again? If so then that would
explain it. id numbers are not reused. Whatever the reason do not
worry about it, you should never assume anything about the actual
value of an id, just consider it to be a random value.
Colin
I can't imagine that being a possibility, but what's stopping you from
looking for yourself?
And if you're concerned that an action is causing duplicate queries
to be run, why not look at the DB logs?
Personally, I have the suspicion that you've used the same database on
that Heroku project for a previous try with this app and it's an
artifact of having previously created a user in that table in the
database. To be perfectly sure, you can drop and recreate the database
on heroku with this command:
heroku run rake db:drop db:create db:migrate
and when that's complete, create your first user again and see what
happens.
If that actually shows that the id's start from one (which my tests do
show), then you have your answer.
Note that on most dbs (eg postgres, mysql) if you roll back a transaction then that does not rollback the sequence, so you can get gaps in the ids of rows without ever having deleted any rows.
Fred
What happened when you tried creating a new test application?
>> What happened when you tried creating a new test application?
>>
The problem as you've stated it is that your first user gets id=2. The
point of the test is to find the minimum set of conditions that
reproduce that behaviour so you can see why it's happening...
... Then prepare the app for heroku, create a new app at heroku for
this test app, and run it on heroku and see what happens
--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/3246c189-392c-4bef-a12a-b8b51174032a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
On 31 Dec 2015 14:10, "'krfg' via Ruby on Rails: Talk" <rubyonra...@googlegroups.com> wrote:
>
> Well, so thank you for helping me and supporting me up to the solution.
> I am already looking for and reading documentation on transactions, sequences and truncations in Postgres, which is new stuff for me. My next goal is finding how to reset the id sequence in Postgres.
Why? As I said before you should not use the id as a numeric value.
Colin
>
> I seize the opportunity to wish you all a happy new year.
>
> --
> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/a3d59189-ad1f-4e64-a5d9-7145532695fb%40googlegroups.com.