- changed status to resolved
mysql patch for a more flexible has_table() function
Issue #557
resolved
becuase show_table_status
acts really weird, particularly on old versions, with unicode names, and external schemas, consider this patch which will just do a "DESCRIBE" and catch the error if table doesnt exist.
Index: lib/sqlalchemy/databases/mysql.py
===================================================================
--- lib/sqlalchemy/databases/mysql.py (revision 2550)
+++ lib/sqlalchemy/databases/mysql.py (working copy)
@@ -364,12 +364,25 @@
def has_table(self, connection, table_name, schema=None):
# TODO: this does not work for table names that contain multibyte characters.
- # i have tried dozens of approaches here with no luck. statements like
- # DESCRIBE and SHOW CREATE TABLE work better, but they raise an error when
- # the table does not exist.
- cursor = connection.execute("show table status like %s", [table_name](table_name))
- return bool( not not cursor.rowcount )
+ # http://dev.mysql.com/doc/refman/5.0/en/error-messages-server.html
+
+ # Error: 1146 SQLSTATE: 42S02 (ER_NO_SUCH_TABLE)
+ # Message: Table '%s.%s' doesn't exist
+
+ # Error: 1046 SQLSTATE: 3D000 (ER_NO_DB_ERROR)
+ # Message: No database selected
+
+ try:
+ name = schema and ("%s.%s" % (schema, table_name)) or table_name
+ connection.execute("DESCRIBE %s" % name)
+ return True
+ except exceptions.SQLError:
+ if e.orig.args[0](0) in (1146, 1046):
+ return False
+ else:
+ raise
+
def reflecttable(self, connection, table):
# reference: http://dev.mysql.com/doc/refman/5.0/en/name-case-sensitivity.html
cs = connection.execute("show variables like 'lower_case_table_names'").fetchone()[1](1)
@@ -377,7 +390,7 @@
cs = cs.tostring()
case_sensitive = int(cs) == 0
- decode_from = connection.execute("show variables like 'character_Set_results'").fetchone()[1](1)
+ decode_from = connection.execute("show variables like 'character_set_results'").fetchone()[1](1)
if not case_sensitive:
table.name = table.name.lower()
Comments (1)
-
- Log in to comment
changeset:2578