sdispater / eloquent

The Eloquent ORM provides a simple yet beautiful ActiveRecord implementation
MIT License
37 stars 3 forks source link

KeyError: 'default' error and 'username' is an invalid keyword argument for this function #15

Closed feleio closed 9 years ago

feleio commented 9 years ago

I have ran code using eloquent package

db.py

from eloquent import DatabaseManager, Model

config = {
    'mysql': {
        'driver': 'mysql',
        'host': 'localhost:33060',
        'database': 'aauuss',
        'username': 'homestead',
        'password': 'secret',
        'prefix': ''
    }
}

db = DatabaseManager(config)
Model.set_connection_resolver(db)

class Source(Model):
    pass

source = Source.all()

and I have got the following error

$ python db,py
Traceback (most recent call last):
  File "test_eloquent.py", line 1, in <module>
    import db
  File "/home/vagrant/Code/aauuss_scrape/db.py", line 70, in <module>
    source = Source.all()
  File "/usr/local/lib/python2.7/dist-packages/eloquent/orm/model.py", line 387, in all
    return instance.new_query().get(columns)
  File "/usr/local/lib/python2.7/dist-packages/eloquent/orm/model.py", line 1241, in new_query
    self._new_base_query_builder()
  File "/usr/local/lib/python2.7/dist-packages/eloquent/orm/model.py", line 1269, in _new_base_query_builder
    conn = self.get_connection()
  File "/usr/local/lib/python2.7/dist-packages/eloquent/orm/model.py", line 2069, in get_connection
    return self.resolve_connection(self.__connection__)
  File "/usr/local/lib/python2.7/dist-packages/eloquent/orm/model.py", line 2103, in resolve_connection
    return cls.__resolver.connection(connection)
  File "/usr/local/lib/python2.7/dist-packages/eloquent/database_manager.py", line 36, in connection
    name, type = self._parse_connection_name(name)
  File "/usr/local/lib/python2.7/dist-packages/eloquent/database_manager.py", line 58, in _parse_connection_name
    name = self.get_default_connection()
  File "/usr/local/lib/python2.7/dist-packages/eloquent/database_manager.py", line 146, in get_default_connection
    return self._config['default']
KeyError: 'default'

I read the source code seeing that I have to call set_default_connection to set it to "mysql"

db.set_default_connection('mysql')

Then I ve got another error.

$ python db.py
Traceback (most recent call last):
  File "test_eloquent.py", line 1, in <module>
    import db
  File "/home/vagrant/Code/aauuss_scrape/db.py", line 71, in <module>
    source = Source.all()
  File "/usr/local/lib/python2.7/dist-packages/eloquent/orm/model.py", line 387, in all
    return instance.new_query().get(columns)
  File "/usr/local/lib/python2.7/dist-packages/eloquent/orm/model.py", line 1241, in new_query
    self._new_base_query_builder()
  File "/usr/local/lib/python2.7/dist-packages/eloquent/orm/model.py", line 1269, in _new_base_query_builder
    conn = self.get_connection()
  File "/usr/local/lib/python2.7/dist-packages/eloquent/orm/model.py", line 2069, in get_connection
    return self.resolve_connection(self.__connection__)
  File "/usr/local/lib/python2.7/dist-packages/eloquent/orm/model.py", line 2103, in resolve_connection
    return cls.__resolver.connection(connection)
  File "/usr/local/lib/python2.7/dist-packages/eloquent/database_manager.py", line 39, in connection
    connection = self._make_connection(name)
  File "/usr/local/lib/python2.7/dist-packages/eloquent/database_manager.py", line 115, in _make_connection
    return self._factory.make(config, name)
  File "/usr/local/lib/python2.7/dist-packages/eloquent/connectors/connection_factory.py", line 22, in make
    return self._create_single_connection(config)
  File "/usr/local/lib/python2.7/dist-packages/eloquent/connectors/connection_factory.py", line 25, in _create_single_connection
    conn = self.create_connector(config).connect(config)
  File "/usr/local/lib/python2.7/dist-packages/eloquent/connectors/mysql_connector.py", line 22, in connect
    return self.get_api().connect(**self.get_config(config))
  File "/usr/lib/python2.7/dist-packages/MySQLdb/__init__.py", line 81, in Connect
    return Connection(*args, **kwargs)
  File "/usr/lib/python2.7/dist-packages/MySQLdb/connections.py", line 187, in __init__
    super(Connection, self).__init__(*args, **kwargs2)
TypeError: 'username' is an invalid keyword argument for this function

I found it is something my mysql connector wont accept the 'username' Could you help me. thanks a lot:)

sdispater commented 9 years ago

Both errors you've encountered are caused by slight omissions and errors in the documentation. The first one is due to the fact that I did not make it clear that you can, in your configuration, add a default connection:

from eloquent import DatabaseManager, Model

config = {
    'default': 'mysql',
    'mysql': {
        'driver': 'mysql',
        'host': 'localhost:33060',
        'database': 'aauuss',
        'username': 'homestead',
        'password': 'secret',
        'prefix': ''
    }
}

db = DatabaseManager(config)
Model.set_connection_resolver(db)

class Source(Model):
    pass

source = Source.all()

I will add it to the documentation and make a change to automatically set the connection if there is only one configured.

As for the second error, it's just that the right keyword is not "username" but "user". So, basically, the correct configuration would be:

from eloquent import DatabaseManager, Model

config = {
    'default': 'mysql',
    'mysql': {
        'driver': 'mysql',
        'host': 'localhost:33060',
        'database': 'aauuss',
        'user': 'homestead',
        'password': 'secret',
        'prefix': ''
    }
}

db = DatabaseManager(config)
Model.set_connection_resolver(db)

class Source(Model):
    pass

source = Source.all()

Thanks for pointing it out. I will fix the documentation.

feleio commented 9 years ago

Hi sdispater,

Thanks a lot for you help!

I have got another error after i have correct the default connection and 'user' it is about autocommit

db.py

from eloquent import DatabaseManager, Model

config = {
    'default': 'mysql',
    'mysql': {
        'driver': 'mysql',
        'host': 'localhost',
        'port': 33060,
        'database': 'aauuss',
        'user': 'homestead',
        'password': 'secret',
        'prefix': ''
    }
}

db = DatabaseManager(config)
Model.set_connection_resolver(db)

class Source(Model):
    pass

source = Source.all()

result:

$ python db.py
Traceback (most recent call last):
  File "test_eloquent.py", line 1, in <module>
    import db
  File "/home/vagrant/Code/aauuss_scrape/db.py", line 72, in <module>
    source = Source.all()
  File "/usr/local/lib/python2.7/dist-packages/eloquent/orm/model.py", line 387, in all
    return instance.new_query().get(columns)
  File "/usr/local/lib/python2.7/dist-packages/eloquent/orm/model.py", line 1241, in new_query
    self._new_base_query_builder()
  File "/usr/local/lib/python2.7/dist-packages/eloquent/orm/model.py", line 1269, in _new_base_query_builder
    conn = self.get_connection()
  File "/usr/local/lib/python2.7/dist-packages/eloquent/orm/model.py", line 2069, in get_connection
    return self.resolve_connection(self.__connection__)
  File "/usr/local/lib/python2.7/dist-packages/eloquent/orm/model.py", line 2103, in resolve_connection
    return cls.__resolver.connection(connection)
  File "/usr/local/lib/python2.7/dist-packages/eloquent/database_manager.py", line 39, in connection
    connection = self._make_connection(name)
  File "/usr/local/lib/python2.7/dist-packages/eloquent/database_manager.py", line 115, in _make_connection
    return self._factory.make(config, name)
  File "/usr/local/lib/python2.7/dist-packages/eloquent/connectors/connection_factory.py", line 22, in make
    return self._create_single_connection(config)
  File "/usr/local/lib/python2.7/dist-packages/eloquent/connectors/connection_factory.py", line 25, in _create_single_connection
    conn = self.create_connector(config).connect(config)
  File "/usr/local/lib/python2.7/dist-packages/eloquent/connectors/mysql_connector.py", line 22, in connect
    return self.get_api().connect(**self.get_config(config))
  File "/usr/lib/python2.7/dist-packages/MySQLdb/__init__.py", line 81, in Connect
    return Connection(*args, **kwargs)
  File "/usr/lib/python2.7/dist-packages/MySQLdb/connections.py", line 187, in __init__
    super(Connection, self).__init__(*args, **kwargs2)
TypeError: 'autocommit' is an invalid keyword argument for this function

I m using MySQLdb and it seems doesn't accept config 'autocommit'?

Thanks again.

sdispater commented 9 years ago

It seems that MySQLdb does not support the autocommit keyword while PyMySQL does which is the one I tested Eloquent with. The fix should not be too complicated but I won't be able to address the issue before tomorrow.

sdispater commented 9 years ago

The compatibility issues with mysqldb have been fixed in version 0.3.1. However, the autocommit keyword is supported in the latest version of mysqldb. You should use https://pypi.python.org/pypi/mysqlclient which is a more up-to-date fork of mysql-python.