models.txt 3.67 KB
Newer Older
1 2 3
FAQ: Databases and models
=========================

4 5
.. _faq-see-raw-sql-queries:

6 7 8
How can I see the raw SQL queries Django is running?
----------------------------------------------------

9 10
Make sure your Django :setting:`DEBUG` setting is set to ``True``.
Then, just do this::
11 12 13

    >>> from django.db import connection
    >>> connection.queries
14
    [{'sql': 'SELECT polls_polls.id, polls_polls.question, polls_polls.pub_date FROM polls_polls',
15 16
    'time': '0.002'}]

17 18 19
``connection.queries`` is only available if :setting:`DEBUG` is ``True``.
It's a list of dictionaries in order of query execution. Each dictionary has
the following::
20 21 22 23 24 25

    ``sql`` -- The raw SQL statement
    ``time`` -- How long the statement took to execute, in seconds.

``connection.queries`` includes all SQL statements -- INSERTs, UPDATES,
SELECTs, etc. Each time your app hits the database, the query will be recorded.
26 27
Note that the SQL recorded here may be :ref:`incorrectly quoted under SQLite
<sqlite-connection-queries>`.
28

29
If you are using :doc:`multiple databases</topics/db/multi-db>`, you can use the
30 31 32 33 34
same interface on each member of the ``connections`` dictionary::

    >>> from django.db import connections
    >>> connections['my_db_alias'].queries

35 36 37 38 39 40
If you need to clear the query list manually at any point in your functions,
just call ``reset_queries()``, like this::

    from django.db import reset_queries
    reset_queries()

41 42 43
Can I use Django with a pre-existing database?
----------------------------------------------

44
Yes. See :doc:`Integrating with a legacy database </howto/legacy-databases>`.
45 46 47 48

If I make changes to a model, how do I update the database?
-----------------------------------------------------------

49 50 51
Take a look at Django's support for :mod:`schema migrations
<django.db.migrations>`.

52 53
If you don't mind clearing data, your project's ``manage.py`` utility has a
:djadmin:`flush` option to reset the database to the state it was in
54
immediately after :djadmin:`migrate` was executed.
55 56 57 58 59 60 61 62 63 64 65 66 67

Do Django models support multiple-column primary keys?
------------------------------------------------------

No. Only single-column primary keys are supported.

But this isn't an issue in practice, because there's nothing stopping you from
adding other constraints (using the ``unique_together`` model option or
creating the constraint directly in your database), and enforcing the
uniqueness at that level. Single-column primary keys are needed for things such
as the admin interface to work; e.g., you need a simple way of being able to
specify an object to edit or delete.

68 69 70 71 72 73 74 75 76 77 78 79
Does Django support NoSQL databases?
------------------------------------

NoSQL databases are not officially supported by Django itself. There are,
however, a number of side project and forks which allow NoSQL functionality in
Django, like `Django non-rel`_.

You can also take a look on `the wiki page`_ which discusses some alternatives.

.. _`Django non-rel`: http://django-nonrel.org/
.. _`the wiki page`: https://code.djangoproject.com/wiki/NoSqlSupport

80 81 82 83 84
How do I add database-specific options to my CREATE TABLE statements, such as specifying MyISAM as the table type?
------------------------------------------------------------------------------------------------------------------

We try to avoid adding special cases in the Django code to accommodate all the
database-specific options such as table type, etc. If you'd like to use any of
85 86 87
these options, create a migration with a
:class:`~django.db.migrations.operations.RunSQL` operation that contains
``ALTER TABLE`` statements that do what you want to do.
88 89

For example, if you're using MySQL and want your tables to use the MyISAM table
90
type, use the following SQL::
91 92

    ALTER TABLE myapp_mytable ENGINE=MyISAM;