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

from_model throws ResolutionFailed for models with user-specified autoincrement id fields #3978

Closed
rascalking opened this issue May 8, 2024 · 5 comments
Labels
interop how to play nicely with other packages

Comments

@rascalking
Copy link
Contributor

I'm using hypothesis to test a django rest framework api, and I get a ResolutionFailed exception when trying to use from_model() to generate model instances for my tests. It looks like this is happening because my model's primary key is a models.AutoField that I specified, not one that django created automatically.

I'm pretty sure this line is what's tripping me up. Looking at the change that introduced from_model(), I don't see that same field.auto_created check in its predecessor, models().

Does anyone know why that check was added? Would a PR to remove it be accepted?

@Zac-HD
Copy link
Member

Zac-HD commented May 9, 2024

It's been five years since I looked into that, but iirc the idea was to avoid automatically generating values for auto-incrementing index fields. If you want that to be generated anyway, you can pass e.g. id_=... - the field name, and literal ... as the argument.

If that does in fact solve your problem, I'd prefer to keep the existing behavior because being too 'helpful' has gone wrong here in the past.

@rascalking
Copy link
Contributor Author

Sorry, I did a bad job explaining. I actually have the opposite problem...I want hypothesis to not generate a strategy for my id column, but I don't think I can do that. Because I defined the autoid column myself, instead of letting django do it, the "is this a field I should skip" check fails (because field.auto_created is false, where it's true when django creates the field), and from_model() calls from_field() to generate a strategy for it.

A simplified version of the model:

class Group(models.Model):
    groupid = models.AutoField(primary_key=True)

@Zac-HD
Copy link
Member

Zac-HD commented May 9, 2024

Oh, I see - yeah, this seems like something I'd be happy to accept a PR for. I guess we could do something like and not (field.auto_created or isinstance(field, AutoField))? You're more familiar with Django and this use-case than I am, though.

@Zac-HD Zac-HD added the interop how to play nicely with other packages label May 10, 2024
@rascalking
Copy link
Contributor Author

This was a fun one...it turns out auto_created gets set on a bunch of things I didn't realize, so yay for learning new things while trying to fix a different thing.

The cleanest fix seemed to me to be to add in a and not isinstance(field, dm.AutoField) clause alongside the and not field.auto_created.

@tybug
Copy link
Member

tybug commented May 13, 2024

closed by #3984

@tybug tybug closed this as completed May 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
interop how to play nicely with other packages
Projects
None yet
Development

No branches or pull requests

3 participants