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

Factories doesn't seem to work if a session hasn't been used yet. #153

Closed
jpanganiban opened this issue Aug 12, 2014 · 2 comments
Closed
Labels

Comments

@jpanganiban
Copy link

I'm working with SQLAlchemy (with a scoped_session). Here's what I'm getting:

class TestCase(unittest.TestCase):

    def setUp(self):
        self.obj = ObjectFactory()
        print self.obj.id  # None

    def test_scenario(self):
        print self.obj.id  # None; Weird.
        session.query(Object).first()
        print ObjectFactory().id  # 1; Weird.

EDIT:
setUp doesn't seem to be the causing issue. It seems that _setup_next_sequency() doesn't get any primary keys to use if the session hasn't been used yet.

@jpanganiban jpanganiban changed the title Factories doesn't seem to work inside TestCase.setUp methods. Factories doesn't seem to work if a session hasn't been used yet. Aug 12, 2014
@rbarrois
Copy link
Member

rbarrois commented Sep 3, 2014

Hmm, that would be "yet another automagic sequence counter bug" :/

I'll look into it, thanks for the report.

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