PageRenderTime 34ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

Plain Text | 106 lines | 75 code | 31 blank | 0 comment | 0 complexity | 05311bef9ac7d49b76e5944366117e1d MD5 | raw file
Possible License(s): BSD-3-Clause
  1. FAQ: Databases and models
  2. =========================
  3. .. _faq-see-raw-sql-queries:
  4. How can I see the raw SQL queries Django is running?
  5. ----------------------------------------------------
  6. Make sure your Django :setting:`DEBUG` setting is set to ``True``.
  7. Then, just do this::
  8. >>> from django.db import connection
  9. >>> connection.queries
  10. [{'sql': 'SELECT,polls_polls.question,polls_polls.pub_date FROM polls_polls',
  11. 'time': '0.002'}]
  12. ``connection.queries`` is only available if :setting:`DEBUG` is ``True``.
  13. It's a list of dictionaries in order of query execution. Each dictionary has
  14. the following::
  15. ``sql`` -- The raw SQL statement
  16. ``time`` -- How long the statement took to execute, in seconds.
  17. ``connection.queries`` includes all SQL statements -- INSERTs, UPDATES,
  18. SELECTs, etc. Each time your app hits the database, the query will be recorded.
  19. Note that the raw SQL logged in ``connection.queries`` may not include
  20. parameter quoting. Parameter quoting is performed by the database-specific
  21. backend, and not all backends provide a way to retrieve the SQL after quoting.
  22. .. versionadded:: 1.2
  23. If you are using :doc:`multiple databases</topics/db/multi-db>`, you can use the
  24. same interface on each member of the ``connections`` dictionary::
  25. >>> from django.db import connections
  26. >>> connections['my_db_alias'].queries
  27. Can I use Django with a pre-existing database?
  28. ----------------------------------------------
  29. Yes. See :doc:`Integrating with a legacy database </howto/legacy-databases>`.
  30. If I make changes to a model, how do I update the database?
  31. -----------------------------------------------------------
  32. If you don't mind clearing data, your project's ```` utility has an
  33. option to reset the SQL for a particular application::
  34. reset appname
  35. This drops any tables associated with ``appname`` and recreates them.
  36. If you do care about deleting data, you'll have to execute the ``ALTER TABLE``
  37. statements manually in your database. That's the way we've always done it,
  38. because dealing with data is a very sensitive operation that we've wanted to
  39. avoid automating. That said, there's some work being done to add partially
  40. automated database-upgrade functionality.
  41. Do Django models support multiple-column primary keys?
  42. ------------------------------------------------------
  43. No. Only single-column primary keys are supported.
  44. But this isn't an issue in practice, because there's nothing stopping you from
  45. adding other constraints (using the ``unique_together`` model option or
  46. creating the constraint directly in your database), and enforcing the
  47. uniqueness at that level. Single-column primary keys are needed for things such
  48. as the admin interface to work; e.g., you need a simple way of being able to
  49. specify an object to edit or delete.
  50. How do I add database-specific options to my CREATE TABLE statements, such as specifying MyISAM as the table type?
  51. ------------------------------------------------------------------------------------------------------------------
  52. We try to avoid adding special cases in the Django code to accommodate all the
  53. database-specific options such as table type, etc. If you'd like to use any of
  54. these options, create an :ref:`SQL initial data file <initial-sql>` that
  55. contains ``ALTER TABLE`` statements that do what you want to do. The initial
  56. data files are executed in your database after the ``CREATE TABLE`` statements.
  57. For example, if you're using MySQL and want your tables to use the MyISAM table
  58. type, create an initial data file and put something like this in it::
  59. ALTER TABLE myapp_mytable ENGINE=MyISAM;
  60. As explained in the :ref:`SQL initial data file <initial-sql>` documentation,
  61. this SQL file can contain arbitrary SQL, so you can make any sorts of changes
  62. you need to make.
  63. Why is Django leaking memory?
  64. -----------------------------
  65. Django isn't known to leak memory. If you find your Django processes are
  66. allocating more and more memory, with no sign of releasing it, check to make
  67. sure your :setting:`DEBUG` setting is set to ``False``. If :setting:`DEBUG`
  68. is ``True``, then Django saves a copy of every SQL statement it has executed.
  69. (The queries are saved in ``django.db.connection.queries``. See
  70. `How can I see the raw SQL queries Django is running?`_.)
  71. To fix the problem, set :setting:`DEBUG` to ``False``.
  72. If you need to clear the query list manually at any point in your functions,
  73. just call ``reset_queries()``, like this::
  74. from django import db
  75. db.reset_queries()