(Django multi-database framework: http://docs.djangoproject.com/en/dev/topics/db/multi-db/)
Currently the _get_sql_server_ver() function in DatabaseOperations uses django.db.connection to query the SQL Server version. django.db.connection uses the default database, so this doesn't work if we're querying from a non-default database.
I think the fix for this is fairly easy.. I've been able to hack bits of it to work by passing in the 'connection' parameter to this function.
Eg. "self.connection.ops.sql_server_ver < 2005" becomes "self.connection.ops._get_sql_server_ver(self.connection) < 2005".
Comment #1
Posted on Feb 5, 2010 by Grumpy DogI attempted to patch it using your suggestion, but my QuerySets are always returning an empty list even though the database is populated. This occurs with or without the QuerySet.objects.using clause. The same QuerySets pointing to the same database work well when just using a single database. There must be something else going on.
- django-pyodbc-multidb.diff 2.73KB
Comment #2
Posted on Feb 9, 2010 by Grumpy DogI had mistyped _sql_server_version instead of _get_sql_server_version. The following patch for me is working against the Django trunk (12398) and two MSSql2008 databases from Linux using FreeTDS.
- django-pyodbc-multidb.diff 2.75KB
Comment #3
Posted on Feb 9, 2010 by Grumpy DogI should note that my "default" database was a MySQL database that I connected to with 'django.db.backends.mysql'. I did not try to syncdb to the MSSql databases.
Comment #4
Posted on Mar 2, 2010 by Swift PandaThere are other places where the _get_sql_server_version() call causes problems--especially when accessed via the sql_server_ver property since those cases cannot pass "connection". DatabaseOperations.field_cast_sql() is one such case.
Comment #5
Posted on Mar 3, 2010 by Grumpy DogYou're right. My patch doesn't solve the whole problem. Perhaps the best way to solve this is to specify the version in DATABASE_OPTIONS. I'm not sure that a backend should be querying to find out the version on the fly anyway.
Comment #6
Posted on Mar 3, 2010 by Grumpy DogActually, I think a better solution is to do what the Postgres backend does. The DatabaseOperations object in the Postgres backend also needs to know the database version in order to perform some database specific functions (sql_flush for example).
The Postgres solution is to pass the connection object to the DatabaseOperations constructor. This would solve the problem we have in operations.py where we have to import the connection and we are getting the default connection. We should never need to import the connection because it is part of the object.
Comment #7
Posted on Mar 3, 2010 by Grumpy DogHere's my updated patch to pass the connection object along as I suggested in comment
6. It passed my preliminary tests, but I haven't tested it widely yet.
- django-pyodbc-73.diff 1.74KB
Comment #8
Posted on Mar 18, 2010 by Happy OxI'm successfully using this for a read only model in a production app, along with the patch submitted for issue #76 with no problems. Thanks! I hope this makes it into the trunk soon!
Comment #9
Posted on Mar 22, 2010 by Helpful GiraffeI came to the same conclusion and applied basically the same patch. Been working nicely for me :)
Comment #10
Posted on Apr 26, 2010 by Helpful MonkeyIt's working for me! Thank you!
Comment #11
Posted on Apr 30, 2010 by Swift PandaFixed in revision 180, thanks.
Comment #12
Posted on Apr 30, 2010 by Swift Panda(No comment was entered for this change.)
Status: Fixed
Labels:
Type-Defect
Priority-Medium