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

Reset sequence on an abstract factory #103

Closed
tcmb opened this issue Oct 31, 2013 · 2 comments
Closed

Reset sequence on an abstract factory #103

tcmb opened this issue Oct 31, 2013 · 2 comments
Labels

Comments

@tcmb
Copy link

tcmb commented Oct 31, 2013

Hi, thanks for maintaining this package, I find it really useful!

I have an issue where I try to share a Sequence between two otherwise unrelated Factories and set the shared Sequence to a specific start value.

My setup of factories is in this gist

The two factories should not inherit from each other, as they have no common attributes apart from the one that should be the Sequence. So I decided to make an abstract Factory with just that Sequence attribute and have both non-abstract Factories inherit from it.

Now, I want the Sequence to start from a particular value, so I'm calling reset_sequence() on the abstract Factory, but I'm getting the following error:

AttributeError: type object 'ItemFactory' has no attribute 'FACTORY_FOR'

Apparently, the error occurs because Factory Boy is trying to initialize the Sequence to the highest PK, which of course fails for an abstract Factory which has no associated model. But, I want the Sequence to be initialized to the value I pass in reset_sequence() anyway.

@rbarrois
Copy link
Member

rbarrois commented Nov 7, 2013

Hmm, looks like a bug.

I have lots of issues with the "let's set the sequence counter to the highest available pk" behavior :/

I'll try to fix this soon; in the mean time, setting FACTORY_FOR= on the base, abstract factory should fix your issue without trouble.

rbarrois added a commit that referenced this issue Nov 16, 2014
Related to issues #78, #92, #103, #111, #153, #170

The default value of all sequences is now 0; the automagic
``_setup_next_sequence`` behavior of Django/SQLAlchemy has been removed.

This feature's only goal was to allow the following scenario:

1. Run a Python script that uses MyFactory.create() a couple of times
   (with a unique field based on the sequence counter)
2. Run the same Python script a second time

Without the magical ``_setup_next_sequence``, the Sequence counter would be set
to 0 at the beginning of each script run, so both runs would generate objects
with the same values for the unique field ; thus conflicting and crashing.

The above behavior having only a very limited use and bringing various
issues (hitting the database on ``build()``, problems with non-integer
or composite primary key columns, ...), it has been removed.

It could still be emulated through custom ``_setup_next_sequence``
methods, or by calling ``MyFactory.reset_sequence()``.
@rbarrois
Copy link
Member

This issue has been fixed in 13d310f, and will be part of the 2.5.0 release.

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

No branches or pull requests

2 participants