1. =========================
    
  2. FAQ: Databases and models
    
  3. =========================
    
  4. 
    
  5. .. _faq-see-raw-sql-queries:
    
  6. 
    
  7. How can I see the raw SQL queries Django is running?
    
  8. ====================================================
    
  9. 
    
  10. Make sure your Django :setting:`DEBUG` setting is set to ``True``.
    
  11. Then do this::
    
  12. 
    
  13.     >>> from django.db import connection
    
  14.     >>> connection.queries
    
  15.     [{'sql': 'SELECT polls_polls.id, polls_polls.question, polls_polls.pub_date FROM polls_polls',
    
  16.     'time': '0.002'}]
    
  17. 
    
  18. ``connection.queries`` is only available if :setting:`DEBUG` is ``True``.
    
  19. It's a list of dictionaries in order of query execution. Each dictionary has
    
  20. the following::
    
  21. 
    
  22.     ``sql`` -- The raw SQL statement
    
  23.     ``time`` -- How long the statement took to execute, in seconds.
    
  24. 
    
  25. ``connection.queries`` includes all SQL statements -- INSERTs, UPDATES,
    
  26. SELECTs, etc. Each time your app hits the database, the query will be recorded.
    
  27. 
    
  28. If you are using :doc:`multiple databases</topics/db/multi-db>`, you can use the
    
  29. same interface on each member of the ``connections`` dictionary::
    
  30. 
    
  31.     >>> from django.db import connections
    
  32.     >>> connections['my_db_alias'].queries
    
  33. 
    
  34. If you need to clear the query list manually at any point in your functions,
    
  35. call ``reset_queries()``, like this::
    
  36. 
    
  37.     from django.db import reset_queries
    
  38.     reset_queries()
    
  39. 
    
  40. Can I use Django with a preexisting database?
    
  41. =============================================
    
  42. 
    
  43. Yes. See :doc:`Integrating with a legacy database </howto/legacy-databases>`.
    
  44. 
    
  45. If I make changes to a model, how do I update the database?
    
  46. ===========================================================
    
  47. 
    
  48. Take a look at Django's support for :mod:`schema migrations
    
  49. <django.db.migrations>`.
    
  50. 
    
  51. If you don't mind clearing data, your project's ``manage.py`` utility has a
    
  52. :djadmin:`flush` option to reset the database to the state it was in
    
  53. immediately after :djadmin:`migrate` was executed.
    
  54. 
    
  55. Do Django models support multiple-column primary keys?
    
  56. ======================================================
    
  57. 
    
  58. No. Only single-column primary keys are supported.
    
  59. 
    
  60. But this isn't an issue in practice, because there's nothing stopping you from
    
  61. adding other constraints (using the ``unique_together`` model option or
    
  62. creating the constraint directly in your database), and enforcing the
    
  63. uniqueness at that level. Single-column primary keys are needed for things such
    
  64. as the admin interface to work; e.g., you need a single value to specify
    
  65. an object to edit or delete.
    
  66. 
    
  67. Does Django support NoSQL databases?
    
  68. ====================================
    
  69. 
    
  70. NoSQL databases are not officially supported by Django itself. There are,
    
  71. however, a number of side projects and forks which allow NoSQL functionality in
    
  72. Django.
    
  73. 
    
  74. You can take a look on `the wiki page`_ which discusses some projects.
    
  75. 
    
  76. .. _the wiki page: https://code.djangoproject.com/wiki/NoSqlSupport
    
  77. 
    
  78. How do I add database-specific options to my CREATE TABLE statements, such as specifying MyISAM as the table type?
    
  79. ==================================================================================================================
    
  80. 
    
  81. We try to avoid adding special cases in the Django code to accommodate all the
    
  82. database-specific options such as table type, etc. If you'd like to use any of
    
  83. these options, create a migration with a
    
  84. :class:`~django.db.migrations.operations.RunSQL` operation that contains
    
  85. ``ALTER TABLE`` statements that do what you want to do.
    
  86. 
    
  87. For example, if you're using MySQL and want your tables to use the MyISAM table
    
  88. type, use the following SQL::
    
  89. 
    
  90.     ALTER TABLE myapp_mytable ENGINE=MyISAM;