models.txt 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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.id, 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. If you are using :doc:`multiple databases</topics/db/multi-db>`, you can use the
  20. same interface on each member of the ``connections`` dictionary::
  21. >>> from django.db import connections
  22. >>> connections['my_db_alias'].queries
  23. If you need to clear the query list manually at any point in your functions,
  24. just call ``reset_queries()``, like this::
  25. from django.db import reset_queries
  26. reset_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. Take a look at Django's support for :mod:`schema migrations
  33. <django.db.migrations>`.
  34. If you don't mind clearing data, your project's ``manage.py`` utility has a
  35. :djadmin:`flush` option to reset the database to the state it was in
  36. immediately after :djadmin:`migrate` was executed.
  37. Do Django models support multiple-column primary keys?
  38. ------------------------------------------------------
  39. No. Only single-column primary keys are supported.
  40. But this isn't an issue in practice, because there's nothing stopping you from
  41. adding other constraints (using the ``unique_together`` model option or
  42. creating the constraint directly in your database), and enforcing the
  43. uniqueness at that level. Single-column primary keys are needed for things such
  44. as the admin interface to work; e.g., you need a simple way of being able to
  45. specify an object to edit or delete.
  46. Does Django support NoSQL databases?
  47. ------------------------------------
  48. NoSQL databases are not officially supported by Django itself. There are,
  49. however, a number of side project and forks which allow NoSQL functionality in
  50. Django, like `Django non-rel`_.
  51. You can also take a look on `the wiki page`_ which discusses some alternatives.
  52. .. _`Django non-rel`: http://django-nonrel.org/
  53. .. _`the wiki page`: https://code.djangoproject.com/wiki/NoSqlSupport
  54. How do I add database-specific options to my CREATE TABLE statements, such as specifying MyISAM as the table type?
  55. ------------------------------------------------------------------------------------------------------------------
  56. We try to avoid adding special cases in the Django code to accommodate all the
  57. database-specific options such as table type, etc. If you'd like to use any of
  58. these options, create a migration with a
  59. :class:`~django.db.migrations.operations.RunSQL` operation that contains
  60. ``ALTER TABLE`` statements that do what you want to do.
  61. For example, if you're using MySQL and want your tables to use the MyISAM table
  62. type, use the following SQL::
  63. ALTER TABLE myapp_mytable ENGINE=MyISAM;