Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Django, MySQL - bug?
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  8 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
Tomasz Zieliński  
View profile  
 More options Jul 18 2009, 5:39 pm
From: Tomasz Zieliński <tomasz.zielin...@pyconsultant.eu>
Date: Sat, 18 Jul 2009 14:39:38 -0700 (PDT)
Local: Sat, Jul 18 2009 5:39 pm
Subject: Django, MySQL - bug?
I prepared following repro case, just paste to models.py to an empty
project:

-----------------------------------------------------------
from MySQLdb import Warning as Warning
from django.db import models
import unittest

class TestModel(models.Model):
    char_field = models.CharField(max_length=5)

class Case1(unittest.TestCase):
    def test1(self):
        print "Count 1:", TestModel.objects.count()
        m = TestModel(char_field="123456")
        try:
            m.save()
        except Warning:
            print "Count 2:", TestModel.objects.count()
            print "m.id=", m.id
            print "all()[0].id=", TestModel.objects.all()[0].id
-----------------------------------------------------------

manage.py test returns:

Count 1: 0
Count 2: 1
m.id= None
all()[0].id= 1

Which seems like a bug to me (m.id=None). Am I right or I overlooked
some info in documentation?

--
Tomasz Zieliński
http://pyconsultant.eu


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Daniel Roseman  
View profile  
 More options Jul 19 2009, 4:35 am
From: Daniel Roseman <dan...@roseman.org.uk>
Date: Sun, 19 Jul 2009 01:35:26 -0700 (PDT)
Local: Sun, Jul 19 2009 4:35 am
Subject: Re: Django, MySQL - bug?
On Jul 18, 10:39 pm, Tomasz Zieliński

I don't know why you think it's a bug. Django's models are - by design
- not an instantaneous reflection of the database. Rather, they only
interact with the DB when you load and save them. So this is the
expected behaviour.
--
DR.

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Tomasz Zieliński  
View profile  
 More options Jul 19 2009, 6:42 am
From: Tomasz Zieliński <tomasz.zielin...@pyconsultant.eu>
Date: Sun, 19 Jul 2009 03:42:31 -0700 (PDT)
Local: Sun, Jul 19 2009 6:42 am
Subject: Re: Django, MySQL - bug?
On 19 Lip, 10:35, Daniel Roseman <dan...@roseman.org.uk> wrote:

> I don't know why you think it's a bug. Django's models are - by design
> - not an instantaneous reflection of the database. Rather, they only
> interact with the DB when you load and save them. So this is the
> expected behaviour.

So, should m.save() set m.id to some other value than None
or this is relaxed when Warning exception is thrown?

--
Tomasz Zieliński
http://pyconsultant.eu


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Joshua Russo  
View profile  
 More options Jul 19 2009, 10:29 am
From: Joshua Russo <josh.r.ru...@gmail.com>
Date: Sun, 19 Jul 2009 13:29:29 -0100
Local: Sun, Jul 19 2009 10:29 am
Subject: Re: Django, MySQL - bug?

2009/7/19 Tomasz Zieliński <tomasz.zielin...@pyconsultant.eu>

> So, should m.save() set m.id to some other value than None
> or this is relaxed when Warning exception is thrown?

It's the database that gives m.id a value upon a valid save. So because the
record couldn't be saved it never received a value.

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Tomasz Zieliński  
View profile  
 More options Jul 19 2009, 11:41 am
From: Tomasz Zieliński <tomasz.zielin...@pyconsultant.eu>
Date: Sun, 19 Jul 2009 08:41:23 -0700 (PDT)
Local: Sun, Jul 19 2009 11:41 am
Subject: Re: Django, MySQL - bug?

On 19 Lip, 16:29, Joshua Russo <josh.r.ru...@gmail.com> wrote:

> 2009/7/19 Tomasz Zieliński <tomasz.zielin...@pyconsultant.eu>

> > So, should m.save() set m.id to some other value than None
> > or this is relaxed when Warning exception is thrown?

> It's the database that gives m.id a value upon a valid save. So because the
> record couldn't be saved it never received a value.

The record is saved - what is raised is Warning (about string being
truncated), not Error.

As a proof, following instruction prints 1:

print "all()[0].id=", TestModel.objects.all()[0].id

--
Tomasz Zieliński
http://pyconsultant.eu


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Alex Gaynor  
View profile  
 More options Jul 19 2009, 12:32 pm
From: Alex Gaynor <alex.gay...@gmail.com>
Date: Sun, 19 Jul 2009 11:32:21 -0500
Local: Sun, Jul 19 2009 12:32 pm
Subject: Re: Django, MySQL - bug?
2009/7/19 Tomasz Zieliński <tomasz.zielin...@pyconsultant.eu>:

I believe this is just a shortcoming of either MySQL, or MySQLDb, in
that it can't return the ID of an inserted object.

Alex

--
"I disapprove of what you say, but I will defend to the death your
right to say it." -- Voltaire
"The people's good is the highest law." -- Cicero
"Code can always be simpler than you think, but never as simple as you
want" -- Me


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Tomasz Zieliński  
View profile  
 More options Jul 19 2009, 6:29 pm
From: Tomasz Zieliński <tomasz.zielin...@pyconsultant.eu>
Date: Sun, 19 Jul 2009 15:29:10 -0700 (PDT)
Local: Sun, Jul 19 2009 6:29 pm
Subject: Re: Django, MySQL - bug?
On 19 Lip, 18:32, Alex Gaynor <alex.gay...@gmail.com> wrote:

Thanks, I'll check it down the pipeline then.

--
Tomasz Zieliński
http://pyconsultant.eu


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Tomasz Zieliński  
View profile  
 More options Jul 20 2009, 7:13 am
From: Tomasz Zieliński <tomasz.zielin...@pyconsultant.eu>
Date: Mon, 20 Jul 2009 04:13:13 -0700 (PDT)
Local: Mon, Jul 20 2009 7:13 am
Subject: Re: Django, MySQL - bug?
I tried this:

import MySQLdb
db = MySQLdb.connect(...)
c = db.cursor()
c.execute("create table mtest (pk int not null auto_increment primary
key, s varchar(5)) type=InnoDB")

print c.execute("insert into mtest values (null,'12345')")
print c.messages
print c.lastrowid

print c.execute("insert into mtest values (null,'123456789')")
print c.messages
print c.lastrowid

And I got this:

1
[]
21

1
[(<class '_mysql_exceptions.Warning'>, ('Warning', 1265L, "Data
truncated for column 's' at row 1"))]
22

So it seems to my MySQLdb actually returns primary key for INSERT that
causes Warning.

One thing that is different here from Django scenario is that Warning
is not being thrown,
does someone know how to turn on raising Warning exceptions in
MySQLdb?

--
Tomasz Zieliński
http://pyconsultant.eu


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »