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

date is None with MySQL #1086

Closed
albertz opened this issue Oct 4, 2016 · 4 comments
Closed

date is None with MySQL #1086

albertz opened this issue Oct 4, 2016 · 4 comments

Comments

@albertz
Copy link

albertz commented Oct 4, 2016

Via pwiz on my MySQL database, I get:

class BaseModel(Model):
    class Meta:
        database = database

class Pub(BaseModel):
    ...
    author = TextField(null=True)
    ...
    publish_date = DateField(null=True)
    ...

Then, when iterating entry in Pub.select(), entry.publish_date is always None, although all entries in the database have the date set (or at least part of it, such as year, i.e. an entry like 2016-00-00).

Why is that? Maybe formats is wrong? How to fix this?

I guess that peewee will use pymsql under the hood. So I tried to do this simple thing:

import pymysql
conn = pymysql.connect(...)
cur = conn.cursor()
cur.execute("SELECT publish_date FROM pub")

And then iterating row in cur, I will only get row == (None,).

So, not sure if this is a bug in peewee or in pymysql.

When casting the value beforehand to char, it works, i.e. I get the value as a string:

cur.execute("SELECT CAST(publish_date AS char) FROM pub")

So, should peewee automatically do this? How can I do that myself now as a work around?

(Also asked on StackOverflow here.)

@coleifer
Copy link
Owner

coleifer commented Oct 4, 2016

Peewee is using pymysql under-the-hood (or MySQLdb, whichever you've got installed), so when you are querying the value using pymysql directly and not receiving anything that tells me that the issue is not with Peewee but somewhere else. Perhaps with pymysql though I'm not sure.

@albertz
Copy link
Author

albertz commented Oct 5, 2016

I reported it to pymysql. I guess you are right, it should be fixed in pymysql, but it might make sense to also provide a work-around in peewee. Or at least, for now when this is not fixed, how would an easy work-around look like to use this in peewee? Basically doing SELECT CAST(publish_date AS char) FROM pub.

@albertz
Copy link
Author

albertz commented Oct 5, 2016

The problem is that the datetime object does not allow something like (2016, 0, 0). That's why pymysql currently failed to convert it and return it. It is possible to disable this conversation in pymysql, but then, I think peewee has the same problem. Currently, peewee will just pass through the string and not convert it properly. As far as I understand, something like entry.publish_date.year should normally work, but it does not for my case because entry.publish_date is a string. Do you consider that as a bug in peewee?

@coleifer
Copy link
Owner

coleifer commented Oct 5, 2016

Not at all a bug in peewee.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants