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

if create fails, it still auto increment #7

Open
bbc4468 opened this issue Feb 12, 2014 · 10 comments
Open

if create fails, it still auto increment #7

bbc4468 opened this issue Feb 12, 2014 · 10 comments

Comments

@bbc4468
Copy link

bbc4468 commented Feb 12, 2014

I have the following code

class SizeList
include Mongoid::Document
field :_id, type: Integer
field :name
auto_increment :sequence, :seed => -1, :step => 1
before_create { self.id = self.sequence }
end

I want the sequence to be continuous. This works really well, except in the case when create fails. It should decrease the sequence in such cases.

You should probably use after_add callback to increase the auto increment value.

Although i can still live with it.

@proton proton self-assigned this Feb 14, 2014
@proton proton added the bug label Feb 14, 2014
@proton proton removed their assignment Oct 28, 2017
@0sc
Copy link

0sc commented Oct 7, 2018

Hi @proton, I'm looking to take this on. What do you think of changing https://github.com/proton/mongoid_auto_increment/blob/master/lib/mongoid_auto_increment.rb#L17 to after_create instead? That way the sequence is only generated when the DB persistence is confirmed

@proton
Copy link
Owner

proton commented Oct 7, 2018

@0sc can you tell me more?
If we just move this code to after_create it will create objects with nil id.

@0sc
Copy link

0sc commented Oct 7, 2018

@proton my understanding is that if create fails the @@incrementor.inc(seq_name, options)) logic never executes and @@incrementor will remain in the state it was in before the creation was attempted. That way only valid/persisted get to have sequence assigned to them.

@proton
Copy link
Owner

proton commented Oct 7, 2018

@0sc so what you propose?

@0sc
Copy link

0sc commented Oct 7, 2018

As per this issue, I suggest using after_create instead so that sequences are only assigned to records that will successfully be persisted.

@proton
Copy link
Owner

proton commented Oct 7, 2018

@0sc so you want make it in two transactions? I prefer keep atomicity.

@0sc
Copy link

0sc commented Oct 7, 2018

I'm not sure I understand what you mean. Activerecord callbacks are executed within a single database transaction (See the transaction section here). Maybe I'm missing something but switching from before_create to after_create should not introduce another transaction

@proton
Copy link
Owner

proton commented Oct 7, 2018

@0sc it's not ActiveRecord :)

@0sc
Copy link

0sc commented Oct 7, 2018

Ahh, my bad I was looking at the wrong docs.

But why will switching from before_create to after_create introduce another transaction in mongoid?

@proton
Copy link
Owner

proton commented Oct 7, 2018

There are no transactions in mongoid :3

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

No branches or pull requests

3 participants