2016-01-03 11:56:22 +01:00
|
|
|
=========================
|
2008-08-24 00:25:40 +02:00
|
|
|
FAQ: Databases and models
|
|
|
|
=========================
|
|
|
|
|
2010-01-16 04:13:16 +01:00
|
|
|
.. _faq-see-raw-sql-queries:
|
|
|
|
|
2008-08-24 00:25:40 +02:00
|
|
|
How can I see the raw SQL queries Django is running?
|
2016-01-03 11:56:22 +01:00
|
|
|
====================================================
|
2008-08-24 00:25:40 +02:00
|
|
|
|
2011-05-29 19:41:04 +02:00
|
|
|
Make sure your Django :setting:`DEBUG` setting is set to ``True``.
|
2019-06-17 16:54:55 +02:00
|
|
|
Then do this:
|
2023-02-09 16:48:46 +01:00
|
|
|
|
2019-06-17 16:54:55 +02:00
|
|
|
.. code-block:: pycon
|
2008-08-24 00:25:40 +02:00
|
|
|
|
|
|
|
>>> from django.db import connection
|
|
|
|
>>> connection.queries
|
2012-09-08 17:00:04 +02:00
|
|
|
[{'sql': 'SELECT polls_polls.id, polls_polls.question, polls_polls.pub_date FROM polls_polls',
|
2008-08-24 00:25:40 +02:00
|
|
|
'time': '0.002'}]
|
|
|
|
|
2011-05-29 19:41:04 +02:00
|
|
|
``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:
|
2008-08-24 00:25:40 +02:00
|
|
|
|
|
|
|
* ``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.
|
|
|
|
|
2010-08-19 21:27:44 +02:00
|
|
|
If you are using :doc:`multiple databases</topics/db/multi-db>`, you can use the
|
2010-05-09 06:50:48 +02:00
|
|
|
same interface on each member of the ``connections`` dictionary:
|
2023-02-09 16:48:46 +01:00
|
|
|
|
2010-05-09 06:50:48 +02:00
|
|
|
.. code-block:: pycon
|
|
|
|
|
|
|
|
>>> from django.db import connections
|
|
|
|
>>> connections["my_db_alias"].queries
|
|
|
|
|
2014-06-07 14:09:27 +02:00
|
|
|
If you need to clear the query list manually at any point in your functions,
|
2019-06-17 16:54:55 +02:00
|
|
|
call ``reset_queries()``, like this::
|
2014-06-07 14:09:27 +02:00
|
|
|
|
|
|
|
from django.db import reset_queries
|
2023-02-28 20:53:28 +01:00
|
|
|
|
2014-06-07 14:09:27 +02:00
|
|
|
reset_queries()
|
|
|
|
|
2022-03-11 09:11:42 +01:00
|
|
|
Can I use Django with a preexisting database?
|
|
|
|
=============================================
|
2008-08-24 00:25:40 +02:00
|
|
|
|
2010-08-19 21:27:44 +02:00
|
|
|
Yes. See :doc:`Integrating with a legacy database </howto/legacy-databases>`.
|
2008-08-24 00:25:40 +02:00
|
|
|
|
|
|
|
If I make changes to a model, how do I update the database?
|
2016-01-03 11:56:22 +01:00
|
|
|
===========================================================
|
2008-08-24 00:25:40 +02:00
|
|
|
|
2013-10-01 16:39:03 +02:00
|
|
|
Take a look at Django's support for :mod:`schema migrations
|
|
|
|
<django.db.migrations>`.
|
|
|
|
|
2012-06-21 02:03:06 +02:00
|
|
|
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
|
2013-11-21 15:04:31 +01:00
|
|
|
immediately after :djadmin:`migrate` was executed.
|
2008-08-24 00:25:40 +02:00
|
|
|
|
|
|
|
Do Django models support multiple-column primary keys?
|
2016-01-03 11:56:22 +01:00
|
|
|
======================================================
|
2008-08-24 00:25:40 +02:00
|
|
|
|
|
|
|
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
|
2019-06-17 16:54:55 +02:00
|
|
|
as the admin interface to work; e.g., you need a single value to specify
|
|
|
|
an object to edit or delete.
|
2008-08-24 00:25:40 +02:00
|
|
|
|
2014-08-11 13:34:48 +02:00
|
|
|
Does Django support NoSQL databases?
|
2016-01-03 11:56:22 +01:00
|
|
|
====================================
|
2014-08-11 13:34:48 +02:00
|
|
|
|
|
|
|
NoSQL databases are not officially supported by Django itself. There are,
|
2019-11-27 07:36:20 +01:00
|
|
|
however, a number of side projects and forks which allow NoSQL functionality in
|
|
|
|
Django.
|
2014-08-11 13:34:48 +02:00
|
|
|
|
2019-11-27 07:36:20 +01:00
|
|
|
You can take a look on `the wiki page`_ which discusses some projects.
|
2014-08-11 13:34:48 +02:00
|
|
|
|
2019-11-27 07:36:20 +01:00
|
|
|
.. _the wiki page: https://code.djangoproject.com/wiki/NoSqlSupport
|
2014-08-11 13:34:48 +02:00
|
|
|
|
2008-08-24 00:25:40 +02:00
|
|
|
How do I add database-specific options to my CREATE TABLE statements, such as specifying MyISAM as the table type?
|
2016-01-03 11:56:22 +01:00
|
|
|
==================================================================================================================
|
2008-08-24 00:25:40 +02:00
|
|
|
|
|
|
|
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
|
2014-08-11 13:34:48 +02:00
|
|
|
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.
|
2008-08-24 00:25:40 +02:00
|
|
|
|
|
|
|
For example, if you're using MySQL and want your tables to use the MyISAM table
|
2014-08-11 13:34:48 +02:00
|
|
|
type, use the following SQL:
|
2023-02-09 16:48:46 +01:00
|
|
|
|
2014-08-11 13:34:48 +02:00
|
|
|
.. code-block:: sql
|
2008-08-24 00:25:40 +02:00
|
|
|
|
|
|
|
ALTER TABLE myapp_mytable ENGINE=MyISAM;
|