1 FAQ: Databases and models
2 =========================
4 .. _faq-see-raw-sql-queries:
6 How can I see the raw SQL queries Django is running?
7 ----------------------------------------------------
9 Make sure your Django :setting:`DEBUG` setting is set to ``True``.
12 >>> from django.db import connection
13 >>> connection.queries
14 [{'sql': 'SELECT polls_polls.id,polls_polls.question,polls_polls.pub_date FROM polls_polls',
17 ``connection.queries`` is only available if :setting:`DEBUG` is ``True``.
18 It's a list of dictionaries in order of query execution. Each dictionary has
21 ``sql`` -- The raw SQL statement
22 ``time`` -- How long the statement took to execute, in seconds.
24 ``connection.queries`` includes all SQL statements -- INSERTs, UPDATES,
25 SELECTs, etc. Each time your app hits the database, the query will be recorded.
26 Note that the SQL recorded here may be :ref:`incorrectly quoted under SQLite
27 <sqlite-connection-queries>`.
31 If you are using :doc:`multiple databases</topics/db/multi-db>`, you can use the
32 same interface on each member of the ``connections`` dictionary::
34 >>> from django.db import connections
35 >>> connections['my_db_alias'].queries
37 Can I use Django with a pre-existing database?
38 ----------------------------------------------
40 Yes. See :doc:`Integrating with a legacy database </howto/legacy-databases>`.
42 If I make changes to a model, how do I update the database?
43 -----------------------------------------------------------
45 If you don't mind clearing data, your project's ``manage.py`` utility has a
46 :djadmin:`flush` option to reset the database to the state it was in
47 immediately after :djadmin:`syncdb` was executed.
49 If you do care about deleting data, you'll have to execute the ``ALTER TABLE``
50 statements manually in your database.
52 There are `external projects which handle schema updates
53 <http://www.djangopackages.com/grids/g/database-migration/>`_, of which the current
54 defacto standard is `south <http://south.aeracode.org/>`_.
56 Do Django models support multiple-column primary keys?
57 ------------------------------------------------------
59 No. Only single-column primary keys are supported.
61 But this isn't an issue in practice, because there's nothing stopping you from
62 adding other constraints (using the ``unique_together`` model option or
63 creating the constraint directly in your database), and enforcing the
64 uniqueness at that level. Single-column primary keys are needed for things such
65 as the admin interface to work; e.g., you need a simple way of being able to
66 specify an object to edit or delete.
68 How do I add database-specific options to my CREATE TABLE statements, such as specifying MyISAM as the table type?
69 ------------------------------------------------------------------------------------------------------------------
71 We try to avoid adding special cases in the Django code to accommodate all the
72 database-specific options such as table type, etc. If you'd like to use any of
73 these options, create an :ref:`SQL initial data file <initial-sql>` that
74 contains ``ALTER TABLE`` statements that do what you want to do. The initial
75 data files are executed in your database after the ``CREATE TABLE`` statements.
77 For example, if you're using MySQL and want your tables to use the MyISAM table
78 type, create an initial data file and put something like this in it::
80 ALTER TABLE myapp_mytable ENGINE=MyISAM;
82 As explained in the :ref:`SQL initial data file <initial-sql>` documentation,
83 this SQL file can contain arbitrary SQL, so you can make any sorts of changes
86 Why is Django leaking memory?
87 -----------------------------
89 Django isn't known to leak memory. If you find your Django processes are
90 allocating more and more memory, with no sign of releasing it, check to make
91 sure your :setting:`DEBUG` setting is set to ``False``. If :setting:`DEBUG`
92 is ``True``, then Django saves a copy of every SQL statement it has executed.
94 (The queries are saved in ``django.db.connection.queries``. See
95 `How can I see the raw SQL queries Django is running?`_.)
97 To fix the problem, set :setting:`DEBUG` to ``False``.
99 If you need to clear the query list manually at any point in your functions,
100 just call ``reset_queries()``, like this::
102 from django import db