#30445: RunSQL Migration should accept psycogp2 sql.Composable object -------------------------------------+------------------------------------- Reporter: | Owner: nobody suomenusko | Type: New | Status: new feature | Component: | Version: 2.2 Migrations | Keywords: RunSQL Severity: Normal | sql.Composable Triage Stage: | Has patch: 0 Unreviewed | Needs documentation: 0 | Needs tests: 0 Patch needs improvement: 0 | Easy pickings: 0 UI/UX: 0 | -------------------------------------+------------------------------------- Generally, if we want to run raw SQL in the code, you do it the context of a cursor. If you have psycopg2 as your DB backend, in addition to passing cursor.execute a string, you can also pass an sql.Composable object, and it'll work.
However, if you are doing a RunSQL migration, you cannot use these, since the BaseDatabaseSchemaEditor execute method assumes it is a string (?). Specifically, it tries to explicitly casts it to a string, before passing sql to the cursor's execute. It seems like the line is there to account for non string objects, but str() won't convert Composable objects to the SQL statement correctly. I'm not sure how reasonable it is to fix this, especially since I'm not too sure what collect_sql should be doing (it looks like sql being a string is pretty ingrained in the logic), and it's easy enough to fix this yourself by subclassing RunSQL, and calling the Composable's as_string method, but it seems like the schema editor's execute method should be agnostic towards the type you pass in, since all it really does is call the cursor's execute. With that said, I think it's fair to say this is more of a new feature than a bug. -- Ticket URL: <https://code.djangoproject.com/ticket/30445> Django <https://code.djangoproject.com/> The Web framework for perfectionists with deadlines. -- You received this message because you are subscribed to the Google Groups "Django updates" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-updates+unsubscr...@googlegroups.com. To post to this group, send email to django-updates@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-updates/053.b0c7806e99441a83b8178717cfbfd484%40djangoproject.com. For more options, visit https://groups.google.com/d/optout.