db.create_tables causes exception in MySQLdb
See original GitHub issueAfter creating model classes, I attempted to run db.create_tables and received the following error:
Traceback (most recent call last):
File "/Applications/PyCharm.app/Contents/helpers/pydev/pydevd.py", line 1580, in <module>
globals = debugger.run(setup['file'], None, None, is_module)
File "/Applications/PyCharm.app/Contents/helpers/pydev/pydevd.py", line 964, in run
pydev_imports.execfile(file, globals, locals) # execute the script
File "/Users/codylandry/PycharmProjects/servicefusion/web2py/applications/servicefusion2/modules/create_db.py", line 197, in <module>
pw_db.create_tables(tables, safe=True)
File "/Users/codylandry/PycharmProjects/servicefusion/venv/lib/python2.7/site-packages/peewee.py", line 3772, in create_tables
create_model_tables(models, fail_silently=safe)
File "/Users/codylandry/PycharmProjects/servicefusion/venv/lib/python2.7/site-packages/peewee.py", line 5176, in create_model_tables
m.create_table(**create_table_kwargs)
File "/Users/codylandry/PycharmProjects/servicefusion/venv/lib/python2.7/site-packages/peewee.py", line 4857, in create_table
db.create_table(cls)
File "/Users/codylandry/PycharmProjects/servicefusion/venv/lib/python2.7/site-packages/peewee.py", line 3769, in create_table
return self.execute_sql(*qc.create_table(model_class, safe))
File "/Users/codylandry/PycharmProjects/servicefusion/venv/lib/python2.7/site-packages/peewee.py", line 3683, in execute_sql
cursor.execute(sql, params or ())
File "/Users/codylandry/PycharmProjects/servicefusion/venv/lib/python2.7/site-packages/MySQLdb/cursors.py", line 187, in execute
query = query % tuple([db.literal(item) for item in args])
TypeError: not enough arguments for format string
Tracing this to cursors.py I found that the args keyword argument in the execute function is an empty tuple instead of None.
def execute(self, query, args=None):
"""Execute a query.
query -- string, query to execute on server
args -- optional sequence or mapping, parameters to use with query.
Note: If args is a sequence, then %s must be used as the
parameter placeholder in the query. If a mapping is used,
%(key)s must be used as the placeholder.
Returns long integer rows affected, if any
"""
del self.messages[:]
db = self._get_db()
if isinstance(query, unicode):
query = query.encode(db.unicode_literal.charset)
if args is not None:
if isinstance(args, dict):
query = query % dict((key, db.literal(item))
for key, item in args.iteritems())
else:
query = query % tuple([db.literal(item) for item in args])
try:
r = None
r = self._query(query)
except TypeError, m:
if m.args[0] in ("not enough arguments for format string",
"not all arguments converted"):
self.messages.append((ProgrammingError, m.args[0]))
self.errorhandler(self, ProgrammingError, m.args[0])
else:
self.messages.append((TypeError, m))
self.errorhandler(self, TypeError, m)
except (SystemExit, KeyboardInterrupt):
raise
except:
exc, value, tb = sys.exc_info()
del tb
self.messages.append((exc, value))
self.errorhandler(self, exc, value)
self._executed = query
if not self._defer_warnings: self._warning_check()
return r
If this is being caused by my model, I’m unclear as to how. I ran the generated sql and was able to create the table the debugger had broke at, so I believe the model is written correctly. I think this just a matter of passing None if there are no arguments to the execute function.
I found this in peewee.py. I’d submit a PR to change it, but I can only test it against mysql.
def execute_sql(self, sql, params=None, require_commit=True):
logger.debug((sql, params))
with self.exception_wrapper:
cursor = self.get_cursor()
try:
cursor.execute(sql, params or ()) # <== should be params or None ?
except Exception:
if self.autorollback and self.get_autocommit():
self.rollback()
raise
else:
if require_commit and self.get_autocommit():
self.commit()
return cursor
Issue Analytics
- State:
- Created 7 years ago
- Comments:9 (4 by maintainers)
Top Results From Across the Web
Does CREATE TABLE IF NOT EXISTS work in MySQLdb ...
mysql syntax is. CREATE TABLE [IF NOT EXISTS] tbl_name (create_definition,...) [table_options] [partition_options]. using the following.
Read more >DatabaseFacade.EnsureCreated throws 'MySqlDbException'
Database.EnsureCreated(); As the annotation says, the method should return a 'bool' value instead of throwing an exception.
Read more >User Source Automatic not creating table in MySQL DB - Ignition
I have this error “DB_AutomaticMode 08Dec2020 13:22:57 [profileName=Users] Unable to create tables for automatic database user source.”.
Read more >Unable to access mysql db and apply changes using "python ...
Unable to access mysql db and apply changes using "python manage.py migrate" ... The above exception was the direct cause of the following...
Read more >MySQLdb User's Guide — MySQLdb 1.2.4b4 documentation
MySQLdb is an interface to the popular MySQL database server that ... it is possible to filter these out or cause Warning to...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I verified that double % works. Sorry to waste your time. It wasn’t obvious to me at first why that would fail. Thanks for looking into it though.
I have an application that must support both Sqlite and MySQL. How can I make the above
EnumField
compatible withSqliteDatabase
? When I use the optionfield_types={"ENUM": "TEXT"}
, the produced DDL is not valid for Sqlite:Instead I want
customerType TEXT
.