<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>FAQ: Databases and models — Django 1.4.11 documentation</title> <link rel="stylesheet" href="../_static/default.css" type="text/css" /> <link rel="stylesheet" href="../_static/pygments.css" type="text/css" /> <script type="text/javascript"> var DOCUMENTATION_OPTIONS = { URL_ROOT: '../', VERSION: '1.4.11', COLLAPSE_INDEX: false, FILE_SUFFIX: '.html', HAS_SOURCE: true }; </script> <script type="text/javascript" src="../_static/jquery.js"></script> <script type="text/javascript" src="../_static/underscore.js"></script> <script type="text/javascript" src="../_static/doctools.js"></script> <link rel="top" title="Django 1.4.11 documentation" href="../index.html" /> <link rel="up" title="Django FAQ" href="index.html" /> <link rel="next" title="FAQ: The admin" href="admin.html" /> <link rel="prev" title="FAQ: Getting Help" href="help.html" /> <script type="text/javascript" src="../templatebuiltins.js"></script> <script type="text/javascript"> (function($) { if (!django_template_builtins) { // templatebuiltins.js missing, do nothing. return; } $(document).ready(function() { // Hyperlink Django template tags and filters var base = "../ref/templates/builtins.html"; if (base == "#") { // Special case for builtins.html itself base = ""; } // Tags are keywords, class '.k' $("div.highlight\\-html\\+django span.k").each(function(i, elem) { var tagname = $(elem).text(); if ($.inArray(tagname, django_template_builtins.ttags) != -1) { var fragment = tagname.replace(/_/, '-'); $(elem).html("<a href='" + base + "#" + fragment + "'>" + tagname + "</a>"); } }); // Filters are functions, class '.nf' $("div.highlight\\-html\\+django span.nf").each(function(i, elem) { var filtername = $(elem).text(); if ($.inArray(filtername, django_template_builtins.tfilters) != -1) { var fragment = filtername.replace(/_/, '-'); $(elem).html("<a href='" + base + "#" + fragment + "'>" + filtername + "</a>"); } }); }); })(jQuery); </script> </head> <body> <div class="document"> <div id="custom-doc" class="yui-t6"> <div id="hd"> <h1><a href="../index.html">Django 1.4.11 documentation</a></h1> <div id="global-nav"> <a title="Home page" href="../index.html">Home</a> | <a title="Table of contents" href="../contents.html">Table of contents</a> | <a title="Global index" href="../genindex.html">Index</a> | <a title="Module index" href="../py-modindex.html">Modules</a> </div> <div class="nav"> « <a href="help.html" title="FAQ: Getting Help">previous</a> | <a href="index.html" title="Django FAQ" accesskey="U">up</a> | <a href="admin.html" title="FAQ: The admin">next</a> »</div> </div> <div id="bd"> <div id="yui-main"> <div class="yui-b"> <div class="yui-g" id="faq-models"> <div class="section" id="s-faq-databases-and-models"> <span id="faq-databases-and-models"></span><h1>FAQ: Databases and models<a class="headerlink" href="#faq-databases-and-models" title="Permalink to this headline">¶</a></h1> <div class="section" id="s-how-can-i-see-the-raw-sql-queries-django-is-running"> <span id="s-faq-see-raw-sql-queries"></span><span id="how-can-i-see-the-raw-sql-queries-django-is-running"></span><span id="faq-see-raw-sql-queries"></span><h2>How can I see the raw SQL queries Django is running?<a class="headerlink" href="#how-can-i-see-the-raw-sql-queries-django-is-running" title="Permalink to this headline">¶</a></h2> <p>Make sure your Django <a class="reference internal" href="../ref/settings.html#std:setting-DEBUG"><tt class="xref std std-setting docutils literal"><span class="pre">DEBUG</span></tt></a> setting is set to <tt class="docutils literal"><span class="pre">True</span></tt>. Then, just do this:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">django.db</span> <span class="kn">import</span> <span class="n">connection</span> <span class="gp">>>> </span><span class="n">connection</span><span class="o">.</span><span class="n">queries</span> <span class="go">[{'sql': 'SELECT polls_polls.id,polls_polls.question,polls_polls.pub_date FROM polls_polls',</span> <span class="go">'time': '0.002'}]</span> </pre></div> </div> <p><tt class="docutils literal"><span class="pre">connection.queries</span></tt> is only available if <a class="reference internal" href="../ref/settings.html#std:setting-DEBUG"><tt class="xref std std-setting docutils literal"><span class="pre">DEBUG</span></tt></a> is <tt class="docutils literal"><span class="pre">True</span></tt>. It’s a list of dictionaries in order of query execution. Each dictionary has the following:</p> <div class="highlight-python"><pre>``sql`` -- The raw SQL statement ``time`` -- How long the statement took to execute, in seconds.</pre> </div> <p><tt class="docutils literal"><span class="pre">connection.queries</span></tt> includes all SQL statements – INSERTs, UPDATES, SELECTs, etc. Each time your app hits the database, the query will be recorded. Note that the SQL recorded here may be <a class="reference internal" href="../ref/databases.html#sqlite-connection-queries"><em>incorrectly quoted under SQLite</em></a>.</p> <div class="versionadded"> <span class="title">New in Django 1.2:</span> <a class="reference internal" href="../releases/1.2.html"><em>Please see the release notes</em></a></div> <p>If you are using <a class="reference internal" href="../topics/db/multi-db.html"><em>multiple databases</em></a>, you can use the same interface on each member of the <tt class="docutils literal"><span class="pre">connections</span></tt> dictionary:</p> <div class="highlight-python"><div class="highlight"><pre><span class="gp">>>> </span><span class="kn">from</span> <span class="nn">django.db</span> <span class="kn">import</span> <span class="n">connections</span> <span class="gp">>>> </span><span class="n">connections</span><span class="p">[</span><span class="s">'my_db_alias'</span><span class="p">]</span><span class="o">.</span><span class="n">queries</span> </pre></div> </div> </div> <div class="section" id="s-can-i-use-django-with-a-pre-existing-database"> <span id="can-i-use-django-with-a-pre-existing-database"></span><h2>Can I use Django with a pre-existing database?<a class="headerlink" href="#can-i-use-django-with-a-pre-existing-database" title="Permalink to this headline">¶</a></h2> <p>Yes. See <a class="reference internal" href="../howto/legacy-databases.html"><em>Integrating with a legacy database</em></a>.</p> </div> <div class="section" id="s-if-i-make-changes-to-a-model-how-do-i-update-the-database"> <span id="if-i-make-changes-to-a-model-how-do-i-update-the-database"></span><h2>If I make changes to a model, how do I update the database?<a class="headerlink" href="#if-i-make-changes-to-a-model-how-do-i-update-the-database" title="Permalink to this headline">¶</a></h2> <p>If you don’t mind clearing data, your project’s <tt class="docutils literal"><span class="pre">manage.py</span></tt> utility has a <a class="reference internal" href="../ref/django-admin.html#django-admin-flush"><tt class="xref std std-djadmin docutils literal"><span class="pre">flush</span></tt></a> option to reset the database to the state it was in immediately after <a class="reference internal" href="../ref/django-admin.html#django-admin-syncdb"><tt class="xref std std-djadmin docutils literal"><span class="pre">syncdb</span></tt></a> was executed.</p> <p>If you do care about deleting data, you’ll have to execute the <tt class="docutils literal"><span class="pre">ALTER</span> <span class="pre">TABLE</span></tt> statements manually in your database.</p> <p>There are <a class="reference external" href="http://www.djangopackages.com/grids/g/database-migration/">external projects which handle schema updates</a>, of which the current defacto standard is <a class="reference external" href="http://south.aeracode.org/">south</a>.</p> </div> <div class="section" id="s-do-django-models-support-multiple-column-primary-keys"> <span id="do-django-models-support-multiple-column-primary-keys"></span><h2>Do Django models support multiple-column primary keys?<a class="headerlink" href="#do-django-models-support-multiple-column-primary-keys" title="Permalink to this headline">¶</a></h2> <p>No. Only single-column primary keys are supported.</p> <p>But this isn’t an issue in practice, because there’s nothing stopping you from adding other constraints (using the <tt class="docutils literal"><span class="pre">unique_together</span></tt> 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.</p> </div> <div class="section" id="s-how-do-i-add-database-specific-options-to-my-create-table-statements-such-as-specifying-myisam-as-the-table-type"> <span id="how-do-i-add-database-specific-options-to-my-create-table-statements-such-as-specifying-myisam-as-the-table-type"></span><h2>How do I add database-specific options to my CREATE TABLE statements, such as specifying MyISAM as the table type?<a class="headerlink" href="#how-do-i-add-database-specific-options-to-my-create-table-statements-such-as-specifying-myisam-as-the-table-type" title="Permalink to this headline">¶</a></h2> <p>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 these options, create an <a class="reference internal" href="../howto/initial-data.html#initial-sql"><em>SQL initial data file</em></a> that contains <tt class="docutils literal"><span class="pre">ALTER</span> <span class="pre">TABLE</span></tt> statements that do what you want to do. The initial data files are executed in your database after the <tt class="docutils literal"><span class="pre">CREATE</span> <span class="pre">TABLE</span></tt> statements.</p> <p>For example, if you’re using MySQL and want your tables to use the MyISAM table type, create an initial data file and put something like this in it:</p> <div class="highlight-python"><pre>ALTER TABLE myapp_mytable ENGINE=MyISAM;</pre> </div> <p>As explained in the <a class="reference internal" href="../howto/initial-data.html#initial-sql"><em>SQL initial data file</em></a> documentation, this SQL file can contain arbitrary SQL, so you can make any sorts of changes you need to make.</p> </div> <div class="section" id="s-why-is-django-leaking-memory"> <span id="why-is-django-leaking-memory"></span><h2>Why is Django leaking memory?<a class="headerlink" href="#why-is-django-leaking-memory" title="Permalink to this headline">¶</a></h2> <p>Django isn’t known to leak memory. If you find your Django processes are allocating more and more memory, with no sign of releasing it, check to make sure your <a class="reference internal" href="../ref/settings.html#std:setting-DEBUG"><tt class="xref std std-setting docutils literal"><span class="pre">DEBUG</span></tt></a> setting is set to <tt class="docutils literal"><span class="pre">False</span></tt>. If <a class="reference internal" href="../ref/settings.html#std:setting-DEBUG"><tt class="xref std std-setting docutils literal"><span class="pre">DEBUG</span></tt></a> is <tt class="docutils literal"><span class="pre">True</span></tt>, then Django saves a copy of every SQL statement it has executed.</p> <p>(The queries are saved in <tt class="docutils literal"><span class="pre">django.db.connection.queries</span></tt>. See <a class="reference internal" href="#how-can-i-see-the-raw-sql-queries-django-is-running">How can I see the raw SQL queries Django is running?</a>.)</p> <p>To fix the problem, set <a class="reference internal" href="../ref/settings.html#std:setting-DEBUG"><tt class="xref std std-setting docutils literal"><span class="pre">DEBUG</span></tt></a> to <tt class="docutils literal"><span class="pre">False</span></tt>.</p> <p>If you need to clear the query list manually at any point in your functions, just call <tt class="docutils literal"><span class="pre">reset_queries()</span></tt>, like this:</p> <div class="highlight-python"><div class="highlight"><pre><span class="kn">from</span> <span class="nn">django</span> <span class="kn">import</span> <span class="n">db</span> <span class="n">db</span><span class="o">.</span><span class="n">reset_queries</span><span class="p">()</span> </pre></div> </div> </div> </div> </div> </div> </div> <div class="yui-b" id="sidebar"> <div class="sphinxsidebar"> <div class="sphinxsidebarwrapper"> <h3><a href="../contents.html">Table Of Contents</a></h3> <ul> <li><a class="reference internal" href="#">FAQ: Databases and models</a><ul> <li><a class="reference internal" href="#how-can-i-see-the-raw-sql-queries-django-is-running">How can I see the raw SQL queries Django is running?</a></li> <li><a class="reference internal" href="#can-i-use-django-with-a-pre-existing-database">Can I use Django with a pre-existing database?</a></li> <li><a class="reference internal" href="#if-i-make-changes-to-a-model-how-do-i-update-the-database">If I make changes to a model, how do I update the database?</a></li> <li><a class="reference internal" href="#do-django-models-support-multiple-column-primary-keys">Do Django models support multiple-column primary keys?</a></li> <li><a class="reference internal" href="#how-do-i-add-database-specific-options-to-my-create-table-statements-such-as-specifying-myisam-as-the-table-type">How do I add database-specific options to my CREATE TABLE statements, such as specifying MyISAM as the table type?</a></li> <li><a class="reference internal" href="#why-is-django-leaking-memory">Why is Django leaking memory?</a></li> </ul> </li> </ul> <h3>Browse</h3> <ul> <li>Prev: <a href="help.html">FAQ: Getting Help</a></li> <li>Next: <a href="admin.html">FAQ: The admin</a></li> </ul> <h3>You are here:</h3> <ul> <li> <a href="../index.html">Django 1.4.11 documentation</a> <ul><li><a href="index.html">Django FAQ</a> <ul><li>FAQ: Databases and models</li></ul> </li></ul> </li> </ul> <h3>This Page</h3> <ul class="this-page-menu"> <li><a href="../_sources/faq/models.txt" rel="nofollow">Show Source</a></li> </ul> <div id="searchbox" style="display: none"> <h3>Quick search</h3> <form class="search" action="../search.html" method="get"> <input type="text" name="q" /> <input type="submit" value="Go" /> <input type="hidden" name="check_keywords" value="yes" /> <input type="hidden" name="area" value="default" /> </form> <p class="searchtip" style="font-size: 90%"> Enter search terms or a module, class or function name. </p> </div> <script type="text/javascript">$('#searchbox').show(0);</script> </div> </div> <h3>Last update:</h3> <p class="topless">Apr 24, 2014</p> </div> </div> <div id="ft"> <div class="nav"> « <a href="help.html" title="FAQ: Getting Help">previous</a> | <a href="index.html" title="Django FAQ" accesskey="U">up</a> | <a href="admin.html" title="FAQ: The admin">next</a> »</div> </div> </div> <div class="clearer"></div> </div> </body> </html>