blog.notmyidea.org/changing-the-primary-key-of-a-model-in-django.html

83 lines
No EOL
4.1 KiB
HTML

<!DOCTYPE html>
<html lang="fr">
<head>
<title>
Changing the primary key of a model in&nbsp;Django - Alexis Métaireau </title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet"
href="https://blog.notmyidea.org/theme/css/main.css?v2"
type="text/css" />
<link href="https://blog.notmyidea.org/feeds/all.atom.xml"
type="application/atom+xml"
rel="alternate"
title="Alexis Métaireau ATOM Feed" />
</head>
<body>
<div id="content">
<section id="links">
<ul>
<li>
<a class="main" href="/">Alexis Métaireau</a>
</li>
<li>
<a class=""
href="https://blog.notmyidea.org/journal/index.html">Journal</a>
</li>
<li>
<a class="selected"
href="https://blog.notmyidea.org/code/">Code, etc.</a>
</li>
<li>
<a class=""
href="https://blog.notmyidea.org/weeknotes/">Notes hebdo</a>
</li>
<li>
<a class=""
href="https://blog.notmyidea.org/lectures/">Lectures</a>
</li>
<li>
<a class=""
href="https://blog.notmyidea.org/projets.html">Projets</a>
</li>
</ul>
</section>
<header>
<h1 class="post-title">Changing the primary key of a model in&nbsp;Django</h1>
<time datetime="2024-02-22T00:00:00+01:00">22 février 2024</time>
</header>
<article>
<p>I had to change the primary key of a django model, and I wanted to create a migration for this.
The previous model was using django&nbsp;ids.</p>
<p>The migrations will need to do the following&nbsp;things:</p>
<ul>
<li>Create a new <code>uuid</code> field/column in the&nbsp;database</li>
<li>Iterate over the existing items in the table, to generate an uuid for&nbsp;them</li>
<li>Update the eventual references to the model so that it continues to&nbsp;work.</li>
<li>Change the old primary key to a different&nbsp;type</li>
<li>Mark the new primary key as&nbsp;such</li>
</ul>
<p>Create the new field in the&nbsp;model</p>
<div class="highlight"><pre><span></span><code> <span class="n">uuid</span> <span class="o">=</span> <span class="n">models</span><span class="o">.</span><span class="n">UUIDField</span><span class="p">(</span><span class="n">unique</span><span class="o">=</span><span class="kc">True</span><span class="p">,</span> <span class="n">default</span><span class="o">=</span><span class="n">uuid</span><span class="o">.</span><span class="n">uuid4</span><span class="p">,</span> <span class="n">editable</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
</code></pre></div>
<p>To generate the migrations I did <code>django-admin makemigrations</code>. The migration looks like&nbsp;this:</p>
<div class="highlight"><pre><span></span><code>
</code></pre></div>
<p>Running the tests, I figured out that the URLs will need to be updated as well. I found <a href="https://gist.github.com/luzfcb/186ee28b368450035e056228615db999">a regexp for&nbsp;UUIDs</a></p>
<p>
<a href="https://blog.notmyidea.org/tag/django.html">#django</a>, <a href="https://blog.notmyidea.org/tag/orm.html">#orm</a>, <a href="https://blog.notmyidea.org/tag/migrations.html">#migrations</a> - Posté dans la catégorie <a href="https://blog.notmyidea.org/code/">code</a>
</p>
</article>
<footer>
<a id="feed" href="/feeds/all.atom.xml">
<img alt="RSS Logo" src="/theme/rss.svg" />
</a>
</footer>
</div>
</body>
</html>