Quantcast

Integrity Error

classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Integrity Error

Torsten Zander-5
Hi

I have the following model:

class Invoice(models.Model):
    customer = models.ForeignKey(Customer)
    project = models.ForeignKey(Project, blank=True, null=True)
    sum = models.FloatField(default=0)
    tax = models.IntegerField(default=16)
    payed_at = models.DateField(blank=True, null=True)
    payable_at = models.DateField('payable at')
    created_at = models.DateTimeField(auto_now_add=True)

    def invoice_number(self):
        invoice_number = str(self.created_at.year) + '/' +
str(self.id)
        return invoice_number

    def __unicode__(self):
        return unicode(str(self.created_at.year) + '/' + str(self.id))

class InvoiceItem(models.Model):
    invoice = models.ForeignKey(Invoice)
    text = models.TextField()
    unit = models.CharField(max_length=255)
    amount = models.IntegerField()
    sum = models.FloatField()

and try to run this test

 def test_save_invoice(self):
        InvoiceItem.objects.create(sum=12, unit='Stunde', amount=100)
        InvoiceItem.objects.create(sum=10, unit='Stunde', amount=120)
        invoice = Invoice.objects.create(created_at="2011-07-28",
customer_id=1, payable_at='2012-12-12')
        invoice.invoiceitem_set.all()
        self.assertEqual(240, invoice.sum)

with this error:
IntegrityError: invoice_invoiceitem.invoice_id may not be NULL

since it is the relation id Django should take care for the
invoice_invoiceitem.invoice_id

Or is there anything wrong with the code ?

Torsten

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to [hidden email].
To unsubscribe from this group, send email to [hidden email].
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Integrity Error

Thomas Orozco

Your invoice items have no invoice attribute, as the error says.

By the way, although I'm not sure of what you're trying to achieve, let me point out that there is not really a need for a sum field in your invoice as you can just sum all the invoice items' amount (and respect the DRY principle - you use a few other similar issues).

Le 26 août 2011 11:35, "Torsten" <[hidden email]> a écrit :
> Hi
>
> I have the following model:
>
> class Invoice(models.Model):
> customer = models.ForeignKey(Customer)
> project = models.ForeignKey(Project, blank=True, null=True)
> sum = models.FloatField(default=0)
> tax = models.IntegerField(default=16)
> payed_at = models.DateField(blank=True, null=True)
> payable_at = models.DateField('payable at')
> created_at = models.DateTimeField(auto_now_add=True)
>
> def invoice_number(self):
> invoice_number = str(self.created_at.year) + '/' +
> str(self.id)
> return invoice_number
>
> def __unicode__(self):
> return unicode(str(self.created_at.year) + '/' + str(self.id))
>
> class InvoiceItem(models.Model):
> invoice = models.ForeignKey(Invoice)
> text = models.TextField()
> unit = models.CharField(max_length=255)
> amount = models.IntegerField()
> sum = models.FloatField()
>
> and try to run this test
>
> def test_save_invoice(self):
> InvoiceItem.objects.create(sum=12, unit='Stunde', amount=100)
> InvoiceItem.objects.create(sum=10, unit='Stunde', amount=120)
> invoice = Invoice.objects.create(created_at="2011-07-28",
> customer_id=1, payable_at='2012-12-12')
> invoice.invoiceitem_set.all()
> self.assertEqual(240, invoice.sum)
>
> with this error:
> IntegrityError: invoice_invoiceitem.invoice_id may not be NULL
>
> since it is the relation id Django should take care for the
> invoice_invoiceitem.invoice_id
>
> Or is there anything wrong with the code ?
>
> Torsten
>
> --
> You received this message because you are subscribed to the Google Groups "Django users" group.
> To post to this group, send email to [hidden email].
> To unsubscribe from this group, send email to [hidden email].
> For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
>

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to [hidden email].
To unsubscribe from this group, send email to [hidden email].
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Integrity Error

Kejun He
Sometime, set null = true would allow a field to be empty

On Fri, Aug 26, 2011 at 5:41 PM, Thomas Orozco <[hidden email]> wrote:

Your invoice items have no invoice attribute, as the error says.

By the way, although I'm not sure of what you're trying to achieve, let me point out that there is not really a need for a sum field in your invoice as you can just sum all the invoice items' amount (and respect the DRY principle - you use a few other similar issues).

Le 26 août 2011 11:35, "Torsten" <[hidden email]> a écrit :

> Hi
>
> I have the following model:
>
> class Invoice(models.Model):
> customer = models.ForeignKey(Customer)
> project = models.ForeignKey(Project, blank=True, null=True)
> sum = models.FloatField(default=0)
> tax = models.IntegerField(default=16)
> payed_at = models.DateField(blank=True, null=True)
> payable_at = models.DateField('payable at')
> created_at = models.DateTimeField(auto_now_add=True)
>
> def invoice_number(self):
> invoice_number = str(self.created_at.year) + '/' +
> str(self.id)
> return invoice_number
>
> def __unicode__(self):
> return unicode(str(self.created_at.year) + '/' + str(self.id))
>
> class InvoiceItem(models.Model):
> invoice = models.ForeignKey(Invoice)
> text = models.TextField()
> unit = models.CharField(max_length=255)
> amount = models.IntegerField()
> sum = models.FloatField()
>
> and try to run this test
>
> def test_save_invoice(self):
> InvoiceItem.objects.create(sum=12, unit='Stunde', amount=100)
> InvoiceItem.objects.create(sum=10, unit='Stunde', amount=120)
> invoice = Invoice.objects.create(created_at="2011-07-28",
> customer_id=1, payable_at='2012-12-12')
> invoice.invoiceitem_set.all()
> self.assertEqual(240, invoice.sum)
>
> with this error:
> IntegrityError: invoice_invoiceitem.invoice_id may not be NULL
>
> since it is the relation id Django should take care for the
> invoice_invoiceitem.invoice_id
>
> Or is there anything wrong with the code ?
>
> Torsten
>
> --
> You received this message because you are subscribed to the Google Groups "Django users" group.
> To post to this group, send email to [hidden email].
> To unsubscribe from this group, send email to [hidden email].
> For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
>

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to [hidden email].
To unsubscribe from this group, send email to [hidden email].
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to [hidden email].
To unsubscribe from this group, send email to [hidden email].
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Integrity Error

Thomas Orozco

That would be blank = True

Le 26 août 2011 11:58, "Kejun He" <[hidden email]> a écrit :
> Sometime, set null = true would allow a field to be empty
>
> On Fri, Aug 26, 2011 at 5:41 PM, Thomas Orozco <[hidden email]>wrote:
>
>> Your invoice items have no invoice attribute, as the error says.
>>
>> By the way, although I'm not sure of what you're trying to achieve, let me
>> point out that there is not really a need for a sum field in your invoice as
>> you can just sum all the invoice items' amount (and respect the DRY
>> principle - you use a few other similar issues).
>> Le 26 août 2011 11:35, "Torsten" <[hidden email]> a écrit :
>>
>> > Hi
>> >
>> > I have the following model:
>> >
>> > class Invoice(models.Model):
>> > customer = models.ForeignKey(Customer)
>> > project = models.ForeignKey(Project, blank=True, null=True)
>> > sum = models.FloatField(default=0)
>> > tax = models.IntegerField(default=16)
>> > payed_at = models.DateField(blank=True, null=True)
>> > payable_at = models.DateField('payable at')
>> > created_at = models.DateTimeField(auto_now_add=True)
>> >
>> > def invoice_number(self):
>> > invoice_number = str(self.created_at.year) + '/' +
>> > str(self.id)
>> > return invoice_number
>> >
>> > def __unicode__(self):
>> > return unicode(str(self.created_at.year) + '/' + str(self.id))
>> >
>> > class InvoiceItem(models.Model):
>> > invoice = models.ForeignKey(Invoice)
>> > text = models.TextField()
>> > unit = models.CharField(max_length=255)
>> > amount = models.IntegerField()
>> > sum = models.FloatField()
>> >
>> > and try to run this test
>> >
>> > def test_save_invoice(self):
>> > InvoiceItem.objects.create(sum=12, unit='Stunde', amount=100)
>> > InvoiceItem.objects.create(sum=10, unit='Stunde', amount=120)
>> > invoice = Invoice.objects.create(created_at="2011-07-28",
>> > customer_id=1, payable_at='2012-12-12')
>> > invoice.invoiceitem_set.all()
>> > self.assertEqual(240, invoice.sum)
>> >
>> > with this error:
>> > IntegrityError: invoice_invoiceitem.invoice_id may not be NULL
>> >
>> > since it is the relation id Django should take care for the
>> > invoice_invoiceitem.invoice_id
>> >
>> > Or is there anything wrong with the code ?
>> >
>> > Torsten
>> >
>> > --
>> > You received this message because you are subscribed to the Google Groups
>> "Django users" group.
>> > To post to this group, send email to [hidden email].
>> > To unsubscribe from this group, send email to
>> [hidden email].
>> > For more options, visit this group at
>> http://groups.google.com/group/django-users?hl=en.
>> >
>>
>> --
>> You received this message because you are subscribed to the Google Groups
>> "Django users" group.
>> To post to this group, send email to [hidden email].
>> To unsubscribe from this group, send email to
>> [hidden email].
>> For more options, visit this group at
>> http://groups.google.com/group/django-users?hl=en.
>>
>
> --
> You received this message because you are subscribed to the Google Groups "Django users" group.
> To post to this group, send email to [hidden email].
> To unsubscribe from this group, send email to [hidden email].
> For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
>

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to [hidden email].
To unsubscribe from this group, send email to [hidden email].
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Integrity Error

Torsten Zander-5
In reply to this post by Thomas Orozco
Thanks Thomas

your are right concerning the sum there is no need for that.
But you say:
Your invoice items have no invoice attribute, as the error says.

Isn't this line which defines the attribute ?
invoice = models.ForeignKey(Invoice)

Thanks
Torsten

On 26 Aug., 11:41, Thomas Orozco <[hidden email]> wrote:

> Your invoice items have no invoice attribute, as the error says.
>
> By the way, although I'm not sure of what you're trying to achieve, let me
> point out that there is not really a need for a sum field in your invoice as
> you can just sum all the invoice items' amount (and respect the DRY
> principle - you use a few other similar issues).
> Le 26 août 2011 11:35, "Torsten" <[hidden email]> a écrit :
>
>
>
>
>
>
>
> > Hi
>
> > I have the following model:
>
> > class Invoice(models.Model):
> > customer = models.ForeignKey(Customer)
> > project = models.ForeignKey(Project, blank=True, null=True)
> > sum = models.FloatField(default=0)
> > tax = models.IntegerField(default=16)
> > payed_at = models.DateField(blank=True, null=True)
> > payable_at = models.DateField('payable at')
> > created_at = models.DateTimeField(auto_now_add=True)
>
> > def invoice_number(self):
> > invoice_number = str(self.created_at.year) + '/' +
> > str(self.id)
> > return invoice_number
>
> > def __unicode__(self):
> > return unicode(str(self.created_at.year) + '/' + str(self.id))
>
> > class InvoiceItem(models.Model):
> > invoice = models.ForeignKey(Invoice)
> > text = models.TextField()
> > unit = models.CharField(max_length=255)
> > amount = models.IntegerField()
> > sum = models.FloatField()
>
> > and try to run this test
>
> > def test_save_invoice(self):
> > InvoiceItem.objects.create(sum=12, unit='Stunde', amount=100)
> > InvoiceItem.objects.create(sum=10, unit='Stunde', amount=120)
> > invoice = Invoice.objects.create(created_at="2011-07-28",
> > customer_id=1, payable_at='2012-12-12')
> > invoice.invoiceitem_set.all()
> > self.assertEqual(240, invoice.sum)
>
> > with this error:
> > IntegrityError: invoice_invoiceitem.invoice_id may not be NULL
>
> > since it is the relation id Django should take care for the
> > invoice_invoiceitem.invoice_id
>
> > Or is there anything wrong with the code ?
>
> > Torsten
>
> > --
> > You received this message because you are subscribed to the Google Groups
>
> "Django users" group.> To post to this group, send email to [hidden email].
> > To unsubscribe from this group, send email to
>
> [hidden email].> For more options, visit this group at
>
> http://groups.google.com/group/django-users?hl=en.
>
>
>
>
>
>
>
>

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to [hidden email].
To unsubscribe from this group, send email to [hidden email].
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Integrity Error

Thomas Orozco
Actually, the line "invoice = models.ForeignKey(Invoice)" tells Django "An invoice item should always be attached to an invoince".

You must realize that models are what they claim to be... models. They are not items of their own, they just define how you would model a particular item in your database.

The problem in your test scenario is that the actual invoice items that you are creating are attached to no actual invoice : 
InvoiceItem.objects.create(sum=12, unit='Stunde', amount=100)
InvoiceItem.objects.create(
sum=10, unit='Stunde', amount=120)

You would actually need to do it the other way around.
First, create the invoice,
my_test_invoice = Invoice.objects.create(created_at="2011-07-28", customer_id=1, payable_at='2012-12-12')
Then, create the items.
InvoiceItem.objects.create(sum=12, unit='Stunde', amount=100, invoice = my_test_invoice)
InvoiceItem.objects.create(
sum=10, unit='Stunde', amount=120, 
invoice = my_test_invoice)

I actually don't use the create syntax, so you might need to call my_test_invoice's save method before creating the invoice items (In a foreign key relationship, you will always need to save the foreign key item before the item that has a foreign key to it, else it has no id and you get an integrity error).


Thomas

2011/8/26 Torsten <[hidden email]>
Thanks Thomas

your are right concerning the sum there is no need for that.
But you say:
Your invoice items have no invoice attribute, as the error says.

Isn't this line which defines the attribute ?
invoice = models.ForeignKey(Invoice)

Thanks
Torsten

On 26 Aug., 11:41, Thomas Orozco <[hidden email]> wrote:
> Your invoice items have no invoice attribute, as the error says.
>
> By the way, although I'm not sure of what you're trying to achieve, let me
> point out that there is not really a need for a sum field in your invoice as
> you can just sum all the invoice items' amount (and respect the DRY
> principle - you use a few other similar issues).
> Le 26 août 2011 11:35, "Torsten" <[hidden email]> a écrit :
>
>
>
>
>
>
>
> > Hi
>
> > I have the following model:
>
> > class Invoice(models.Model):
> > customer = models.ForeignKey(Customer)
> > project = models.ForeignKey(Project, blank=True, null=True)
> > sum = models.FloatField(default=0)
> > tax = models.IntegerField(default=16)
> > payed_at = models.DateField(blank=True, null=True)
> > payable_at = models.DateField('payable at')
> > created_at = models.DateTimeField(auto_now_add=True)
>
> > def invoice_number(self):
> > invoice_number = str(self.created_at.year) + '/' +
> > str(self.id)
> > return invoice_number
>
> > def __unicode__(self):
> > return unicode(str(self.created_at.year) + '/' + str(self.id))
>
> > class InvoiceItem(models.Model):
> > invoice = models.ForeignKey(Invoice)
> > text = models.TextField()
> > unit = models.CharField(max_length=255)
> > amount = models.IntegerField()
> > sum = models.FloatField()
>
> > and try to run this test
>
> > def test_save_invoice(self):
> > InvoiceItem.objects.create(sum=12, unit='Stunde', amount=100)
> > InvoiceItem.objects.create(sum=10, unit='Stunde', amount=120)
> > invoice = Invoice.objects.create(created_at="2011-07-28",
> > customer_id=1, payable_at='2012-12-12')
> > invoice.invoiceitem_set.all()
> > self.assertEqual(240, invoice.sum)
>
> > with this error:
> > IntegrityError: invoice_invoiceitem.invoice_id may not be NULL
>
> > since it is the relation id Django should take care for the
> > invoice_invoiceitem.invoice_id
>
> > Or is there anything wrong with the code ?
>
> > Torsten
>
> > --
> > You received this message because you are subscribed to the Google Groups
>
> "Django users" group.> To post to this group, send email to [hidden email].
> > To unsubscribe from this group, send email to
>
> [hidden email].> For more options, visit this group at
>
> http://groups.google.com/group/django-users?hl=en.
>
>
>
>
>
>
>
>

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to [hidden email].
To unsubscribe from this group, send email to [hidden email].
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to [hidden email].
To unsubscribe from this group, send email to [hidden email].
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Integrity Error

Thomas Orozco
In reply to this post by Torsten Zander-5
While we're at it, here are a few suggestions.
So long as you can, you should use unicode inside python code. The main logic would be that input should be converted to unicode as soon as possible, and that output should be encoded (likely to UTF-8) as late as possible.

Consequently, you might want to rewrite your invoice methods so that they do the following.
invoice_number = unicode(self.created_at.year) + u'/' + unicode(self.id)
You could also write this the following way:
return u'{0}/{1}'.format( self.created_at.year, self.id ), which would be more elegant (if you use format with a unicode string it calls your object's __unicode__ method, and if you use format with a string, it calls the __str__ method.).

By the way, your __unicode__ method actually returns your invoice's invoice_number, so you might want to have __unicode__ return self.invoice_number().
You could even use a single method for both in your code, but that might not be a very good idea: if one day you want to change your display, if you have __unicode__ return self.invoice_number() you can change it.


Regarding your test code, you will need to add a few details I didn't point out first.
None of your fields have "blank = True", so you're going to have to either give them default values or give them actual values in your tests.

Hope this helps.

Thomas

2011/8/26 Torsten <[hidden email]>
Hi

I have the following model:

class Invoice(models.Model):
   customer = models.ForeignKey(Customer)
   project = models.ForeignKey(Project, blank=True, null=True)
   sum = models.FloatField(default=0)
   tax = models.IntegerField(default=16)
   payed_at = models.DateField(blank=True, null=True)
   payable_at = models.DateField('payable at')
   created_at = models.DateTimeField(auto_now_add=True)

   def invoice_number(self):
       invoice_number = str(self.created_at.year) + '/' +
str(self.id)
       return invoice_number

   def __unicode__(self):
       return unicode(str(self.created_at.year) + '/' + str(self.id))

class InvoiceItem(models.Model):
   invoice = models.ForeignKey(Invoice)
   text = models.TextField()
   unit = models.CharField(max_length=255)
   amount = models.IntegerField()
   sum = models.FloatField()

and try to run this test

 def test_save_invoice(self):
       InvoiceItem.objects.create(sum=12, unit='Stunde', amount=100)
       InvoiceItem.objects.create(sum=10, unit='Stunde', amount=120)
       invoice = Invoice.objects.create(created_at="2011-07-28",
customer_id=1, payable_at='2012-12-12')
       invoice.invoiceitem_set.all()
       self.assertEqual(240, invoice.sum)

with this error:
IntegrityError: invoice_invoiceitem.invoice_id may not be NULL

since it is the relation id Django should take care for the
invoice_invoiceitem.invoice_id

Or is there anything wrong with the code ?

Torsten

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to [hidden email].
To unsubscribe from this group, send email to [hidden email].
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to [hidden email].
To unsubscribe from this group, send email to [hidden email].
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Integrity Error

Torsten Zander-5
Thanks Thomas you helped me a lot these are my first step in python
and django.
And I really starting to like it

Torsten

On 26 Aug., 12:23, Thomas Orozco <[hidden email]> wrote:

> While we're at it, here are a few suggestions.
> So long as you can, you should use unicode inside python code. The main
> logic would be that input should be converted to unicode as soon as
> possible, and that output should be encoded (likely to UTF-8) as late as
> possible.
>
> Consequently, you might want to rewrite your invoice methods so that they do
> the following.
> invoice_number = unicode(self.created_at.year) + u'/' + unicode(self.id)
> You could also write this the following way:
> return u'{0}/{1}'.format( self.created_at.year, self.id ), which would be
> more elegant (if you use format with a unicode string it calls your object's
> __unicode__ method, and if you use format with a string, it calls the
> __str__ method.).
>
> By the way, your __unicode__ method actually returns your invoice's
> invoice_number, so you might want to have __unicode__ return
> self.invoice_number().
> You could even use a single method for both in your code, but that might not
> be a very good idea: if one day you want to change your display, if you have
> __unicode__ return self.invoice_number() you can change it.
>
> Regarding your test code, you will need to add a few details I didn't point
> out first.
> None of your fields have "blank = True", so you're going to have to either
> give them default values or give them actual values in your tests.
>
> Hope this helps.
>
> Thomas
>
> 2011/8/26 Torsten <[hidden email]>
>
>
>
>
>
>
>
> > Hi
>
> > I have the following model:
>
> > class Invoice(models.Model):
> >    customer = models.ForeignKey(Customer)
> >    project = models.ForeignKey(Project, blank=True, null=True)
> >    sum = models.FloatField(default=0)
> >    tax = models.IntegerField(default=16)
> >    payed_at = models.DateField(blank=True, null=True)
> >    payable_at = models.DateField('payable at')
> >    created_at = models.DateTimeField(auto_now_add=True)
>
> >    def invoice_number(self):
> >        invoice_number = str(self.created_at.year) + '/' +
> > str(self.id)
> >        return invoice_number
>
> >    def __unicode__(self):
> >        return unicode(str(self.created_at.year) + '/' + str(self.id))
>
> > class InvoiceItem(models.Model):
> >    invoice = models.ForeignKey(Invoice)
> >    text = models.TextField()
> >    unit = models.CharField(max_length=255)
> >    amount = models.IntegerField()
> >    sum = models.FloatField()
>
> > and try to run this test
>
> >  def test_save_invoice(self):
> >        InvoiceItem.objects.create(sum=12, unit='Stunde', amount=100)
> >        InvoiceItem.objects.create(sum=10, unit='Stunde', amount=120)
> >        invoice = Invoice.objects.create(created_at="2011-07-28",
> > customer_id=1, payable_at='2012-12-12')
> >        invoice.invoiceitem_set.all()
> >        self.assertEqual(240, invoice.sum)
>
> > with this error:
> > IntegrityError: invoice_invoiceitem.invoice_id may not be NULL
>
> > since it is the relation id Django should take care for the
> > invoice_invoiceitem.invoice_id
>
> > Or is there anything wrong with the code ?
>
> > Torsten
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "Django users" group.
> > To post to this group, send email to [hidden email].
> > To unsubscribe from this group, send email to
> > [hidden email].
> > For more options, visit this group at
> >http://groups.google.com/group/django-users?hl=en.

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to [hidden email].
To unsubscribe from this group, send email to [hidden email].
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Integrity Error

Thomas Orozco

Glad I could help!

Le 26 août 2011 12:53, "Torsten" <[hidden email]> a écrit :
> Thanks Thomas you helped me a lot these are my first step in python
> and django.
> And I really starting to like it
>
> Torsten
>
> On 26 Aug., 12:23, Thomas Orozco <[hidden email]> wrote:
>> While we're at it, here are a few suggestions.
>> So long as you can, you should use unicode inside python code. The main
>> logic would be that input should be converted to unicode as soon as
>> possible, and that output should be encoded (likely to UTF-8) as late as
>> possible.
>>
>> Consequently, you might want to rewrite your invoice methods so that they do
>> the following.
>> invoice_number = unicode(self.created_at.year) + u'/' + unicode(self.id)
>> You could also write this the following way:
>> return u'{0}/{1}'.format( self.created_at.year, self.id ), which would be
>> more elegant (if you use format with a unicode string it calls your object's
>> __unicode__ method, and if you use format with a string, it calls the
>> __str__ method.).
>>
>> By the way, your __unicode__ method actually returns your invoice's
>> invoice_number, so you might want to have __unicode__ return
>> self.invoice_number().
>> You could even use a single method for both in your code, but that might not
>> be a very good idea: if one day you want to change your display, if you have
>> __unicode__ return self.invoice_number() you can change it.
>>
>> Regarding your test code, you will need to add a few details I didn't point
>> out first.
>> None of your fields have "blank = True", so you're going to have to either
>> give them default values or give them actual values in your tests.
>>
>> Hope this helps.
>>
>> Thomas
>>
>> 2011/8/26 Torsten <[hidden email]>
>>

>>
>>
>>
>>
>>
>>
>> > Hi
>>
>> > I have the following model:
>>
>> > class Invoice(models.Model):
>> >    customer = models.ForeignKey(Customer)
>> >    project = models.ForeignKey(Project, blank=True, null=True)
>> >    sum = models.FloatField(default=0)
>> >    tax = models.IntegerField(default=16)
>> >    payed_at = models.DateField(blank=True, null=True)
>> >    payable_at = models.DateField('payable at')
>> >    created_at = models.DateTimeField(auto_now_add=True)
>>
>> >    def invoice_number(self):
>> >        invoice_number = str(self.created_at.year) + '/' +
>> > str(self.id)
>> >        return invoice_number
>>
>> >    def __unicode__(self):
>> >        return unicode(str(self.created_at.year) + '/' + str(self.id))
>>
>> > class InvoiceItem(models.Model):
>> >    invoice = models.ForeignKey(Invoice)
>> >    text = models.TextField()
>> >    unit = models.CharField(max_length=255)
>> >    amount = models.IntegerField()
>> >    sum = models.FloatField()
>>
>> > and try to run this test
>>
>> >  def test_save_invoice(self):
>> >        InvoiceItem.objects.create(sum=12, unit='Stunde', amount=100)
>> >        InvoiceItem.objects.create(sum=10, unit='Stunde', amount=120)
>> >        invoice = Invoice.objects.create(created_at="2011-07-28",
>> > customer_id=1, payable_at='2012-12-12')
>> >        invoice.invoiceitem_set.all()
>> >        self.assertEqual(240, invoice.sum)
>>
>> > with this error:
>> > IntegrityError: invoice_invoiceitem.invoice_id may not be NULL
>>
>> > since it is the relation id Django should take care for the
>> > invoice_invoiceitem.invoice_id
>>
>> > Or is there anything wrong with the code ?
>>
>> > Torsten
>>
>> > --
>> > You received this message because you are subscribed to the Google Groups
>> > "Django users" group.
>> > To post to this group, send email to [hidden email].
>> > To unsubscribe from this group, send email to
>> > [hidden email].
>> > For more options, visit this group at
>> >http://groups.google.com/group/django-users?hl=en.
>
> --
> You received this message because you are subscribed to the Google Groups "Django users" group.
> To post to this group, send email to [hidden email].
> To unsubscribe from this group, send email to [hidden email].
> For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
>

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to [hidden email].
To unsubscribe from this group, send email to [hidden email].
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.
Loading...