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

Pony fails with mysql on Python 2.6 #87

Closed
thedrow opened this issue Oct 20, 2014 · 7 comments
Closed

Pony fails with mysql on Python 2.6 #87

thedrow opened this issue Oct 20, 2014 · 7 comments
Assignees
Labels
Milestone

Comments

@thedrow
Copy link

thedrow commented Oct 20, 2014

See this import error: https://travis-ci.org/maxtepkeev/architect/jobs/38495030

@amalashkevich
Copy link
Member

Could you try to launch python2.6 and then do the import:

    from _mysql import string_literal

If the import fails then you need to check what is wrong with MySQLdb installation

@thedrow
Copy link
Author

thedrow commented Oct 22, 2014

Nothing is wrong. Breakage was introduced with 0.5.4.
https://travis-ci.org/maxtepkeev/architect/jobs/38506193

@amalashkevich
Copy link
Member

Trying to figure out where is the problem. Could you launch python2.6 in your testing environment and do the import

    from _mysql import string_literal

What is the result of such import?

@socketpair
Copy link

Thanks for not supporting Centos 6 / RedHATE 6. These distros should die with very outdated Python 2.6.

@maxtepkeev
Copy link

Hi!

I found the cause of the problem, now we just have to figure out how to properly fix it. This has nothing to do with Python 2.6, this problem exists in all Python versions and all Pony versions starting from 0.5.4.

In my code I use

from pymysql import install_as_MySQLdb
install_as_MySQLdb()

which I believe is a perfectly valid way to use pymysql instead of mysqldb. Now, the thing is that in Pony you have the following inside orm/dbproviders/mysql.py:

try:
    import MySQLdb as mysql_module
except ImportError as e:
    try:
        import pymysql as mysql_module
    except ImportError:
        raise ImportError('No module named MySQLdb or pymysql found')
    else:
        import pymysql.converters as mysql_converters
        from pymysql.constants import FIELD_TYPE, FLAG, CLIENT
        mysql_converters.encoders[buffer] = lambda val: mysql_converters.escape_str(str(val))
        mysql_converters.encoders[timedelta] = lambda val: mysql_converters.escape_str(timedelta2str(val))
        mysql_module_name = 'pymysql'
else:
    import MySQLdb.converters as mysql_converters
    from MySQLdb.constants import FIELD_TYPE, FLAG, CLIENT
    mysql_module_name = 'MySQLdb'
    from _mysql import string_literal

What happens here is that because I already installed pymysql as mysqldb using the install_as_MySQLdb() pony thinks that mysqldb is installed in the system and the ImportError exception is not raised which causes the else block to execute which leads to this error.

To fix this we can eliminate the else block and move all it's code to the try block like this:

try:
    import MySQLdb as mysql_module
    import MySQLdb.converters as mysql_converters
    from MySQLdb.constants import FIELD_TYPE, FLAG, CLIENT
    mysql_module_name = 'MySQLdb'
    from _mysql import string_literal
except ImportError as e:
    try:
        import pymysql as mysql_module
    except ImportError:
        raise ImportError('No module named MySQLdb or pymysql found')
    else:
        import pymysql.converters as mysql_converters
        from pymysql.constants import FIELD_TYPE, FLAG, CLIENT
        mysql_converters.encoders[buffer] = lambda val: mysql_converters.escape_str(str(val))
        mysql_converters.encoders[timedelta] = lambda val: mysql_converters.escape_str(timedelta2str(val))
        mysql_module_name = 'pymysql'

That will fix the error and make everybody happy. If you agree I can make a pull request or you can inject this fix by yourself.

What do you think ?

@kozlovsky kozlovsky added the bug label Apr 2, 2015
@kozlovsky kozlovsky added this to the 0.6.2 milestone Apr 2, 2015
@kozlovsky kozlovsky self-assigned this Apr 2, 2015
@kozlovsky
Copy link
Member

Thanks for the patch! I fixed the Pony code according to your suggestion, it will be part of the upcoming release PonyORM 0.6.2

@maxtepkeev
Copy link

Awesome, thanks!

kozlovsky added a commit that referenced this issue Jan 11, 2016
The documentation was moved from this repo to a separate one at https://github.com/ponyorm/pony-doc
The compiled version can be found at https://docs.ponyorm.com

# New features

* Python 3.5 support
* #132, #145: raw_sql() function was added
* #126: Ability to use @db_session with generator functions
* #116: Add support to select by UUID
* Ability to get string SQL statement using the Query.get_sql() method
* New function delete(gen) and Query.delete(bulk=False)
* Now it is possible to override Entity.__init__() and declare custom entity methods

# Backward incompatible changes

* Normalizing table names for symmetric relationships
* Autostrip - automatically remove leading and trailing characters

# Bugfixes

* #87: Pony fails with pymysql installed as MySQLdb
* #118: Pony should reconnect if previous connection was created before process was forked
* #121: Unable to update value of unique attribute
* #122: AssertionError when changing part of a composite key
* #127: a workaround for incorrect pysqlite locking behavior
* #136: Cascade delete does not work correctly for one-to-one relationships
* #141, #143: remove restriction on adding new methods to entities
* #142: Entity.select_random() AssertionError
* #147: Add 'atom_expr' symbol handling for Python 3.5 grammar
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants