Thursday, May 25, 2017

Re: null value in column violates not-null constraint

Let me spell it out:

If the error message complains about the user field

And the code you provided is correct

Then your database is out of sync with your model definition

 

 

On Thursday 25 May 2017 13:53:19 Ismail Sarenkapic wrote:

> Tnx ,

> I know that i can manually select the user, but i want it to be

> automatic-with current user to add the product

>

> > > class Product(models.Model):

> > > seller = models.ForeignKey(settings.AUTH_USER_MODEL)

^^^^^^^^^ Here is the seller field

 

> > >

> > > #user = models.OneToOneField(settings.AUTH_USER_MODEL)

^^^^^^^ There is no user field. form.instance.user = 'foo' does not translate to anything useful.

The FormUserNeededMixin sets Product.user, which simply adds an instance variable, that is not picked up by the ORM, because 'user' is not a field in Product.fields.

 

The error should actually be about a field called 'seller', but because it is not in the form, it is not passed to the ORM and the old table complains about the user field, as it is the one that it still has.

 

--

Melvyn Sopacua

No comments:

Post a Comment