berinhard/model_mommy

Fields with `blank=True` cause IntegrityErrors

ffigiel opened this issue · 6 comments

Given the following model

class Project(models.Model):
    owner = models.ForeignKey(
        settings.AUTH_USER_MODEL,
        related_name='owned_projects',
        blank=True)

this fails

mommy.make('Project')

with the following error

django.db.utils.IntegrityError: NOT NULL constraint failed: projects_project.owner_id

I've just read on the docs that this behavior is intended. I'm not sure if that's good. Adding _fill_optional everywhere feels like an unnecessary hurdle to me.

Shouldn't mommy generate the value for blank=True fields that don't have a default value?

@megapctr No it shouldn't. But you can use this feature (currently in develop branch) #276

So that you can plug in your custom implementation.
:)

Could you elaborate on that?

Indeed. Sorry about not doing it before.
Mommy only generate the minimum necessary values by default. In most cases so far, filling the non required fields (nulls and blanks) isn't needed. BUT sometimes, depending on what you're working on, it's necessary, then we added the parameter _fill_optional.

I understand you have a case where you need/want everything to be filled, but it's not everyone else's case (so far). So, i presented you with the option of providing a custom mommy implementation (#276) which will solve your case.

I see, thanks.

@megapctr thanks for taking your time to help this project :)

BTW, you can upgrade to 1.3.0 if you want.