DjangoModelFactory's "_setup_next_sequence" assumes that pk is an integer
See original GitHub issueOffending code is here:
@classmethod
def _setup_next_sequence(cls):
"""Compute the next available PK, based on the 'pk' database field."""
model = cls._associated_class # pylint: disable=E1101
manager = cls._get_manager(model)
try:
return 1 + manager.values_list('pk', flat=True
).order_by('-pk')[0]
except IndexError:
return 1
This problem didn’t exist in factory_boy 1.3. My field that was using a non-integer PK is using a sequence, which worked fine previously:
code = factory.Sequence(lambda n: str(n).zfill(3))
I haven’t dug into the code enough to know much about the changes that caused this problem, but ultimately I’d like to be able to use the sequence for the field again.
Issue Analytics
- State:
- Created 10 years ago
- Comments:16 (9 by maintainers)
Top Results From Across the Web
Common recipes — Factory Boy stable documentation
Sequence declaration to provide varying values to attributes declared as unique.
Read more >python - How to setup a sequence for an attribute while using ...
When using factory_boy in Django, how can I achieve this? models.py class TestModel(models.Model): name = CharField() order = IntegerField().
Read more >How To Pass In A Starting Sequence Number To A Django ...
DjangoModelFactory's setupnextsequence assumes that pk is an integer #57 Basically the starting number of the sequence should be setup according to.
Read more >Testing Models with Django using Faker and Factory Boy
you can add factory.Sequence. import factoryclass CompanyFactory(factory.DjangoModelFactory): """ | id | name | |:-- ...
Read more >Shoop Documentation - Read the Docs
This guide assumes familiarity with the PyPA tools for Python packaging, ... There's also update instructions for updating from 3.0.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Thanks. After all I think like this is specific to the use case and everybody should write his own code.
No 😃
The goal of
_setup_next_sequence()
is simply to initialize the sequence counter, used infactory.Sequence()
andfactory.LazyAttributeSequence()
. It holds no relation whatsoever with any database primary key — it has no knowledge thereof.If you wish to have it start at a given value, why don’t you use
Factory.reset_sequence()
? Another option is to override the_setup_next_sequence()
function in your factory to fetch a viable next ID from your database.However, I strongly suggest letting your database generate the PK itself — otherwise, it is your job to define how your factory can choose values that are guaranteed not to conflict with those already existing in the DB. In your example, what happens if some part of the code under test creates an entry with PK “foo2” after the factory was called? There is no way for the factory to automagically guess that it has to move on to
foo3
…