question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

DjangoModelFactory's "_setup_next_sequence" assumes that pk is an integer

See original GitHub issue

Offending 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:closed
  • Created 10 years ago
  • Comments:16 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
wieczorek1990commented, Feb 24, 2021

Thanks. After all I think like this is specific to the use case and everybody should write his own code.

0reactions
rbarroiscommented, Feb 24, 2021

No 😃

The goal of _setup_next_sequence() is simply to initialize the sequence counter, used in factory.Sequence() and factory.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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found