How do I get ActiveRecord to show the next id (last + 1) in Ruby on Rails?

前端 未结 13 1264
臣服心动
臣服心动 2020-12-05 06:33

Is there a compact way with ActiveRecord to query for what id it\'s going to use next if an object was going to be persisted to the database? In SQL, a query like this would

13条回答
  •  北海茫月
    2020-12-05 07:03

    I don't think there's a generic answer to this since you may not want to assume one database over another. Oracle, for example, may assign id's by a sequence or, worse, by a trigger.

    As far as other databases are concerned, one may set them up with non sequential or random id allocation.

    May I ask what the use case is? Why would you need to anticipate the next id? What does 'next' mean? Relative to when? What about race conditions (multiuser environments)?

提交回复
热议问题