mirror of
https://github.com/almet/notmyidea.git
synced 2025-04-28 19:42:37 +02:00
83 lines
No EOL
4.1 KiB
HTML
83 lines
No EOL
4.1 KiB
HTML
<!DOCTYPE html>
|
|
<html lang="fr">
|
|
<head>
|
|
<title>
|
|
Changing the primary key of a model in 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 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 ids.</p>
|
|
<p>The migrations will need to do the following things:</p>
|
|
<ul>
|
|
<li>Create a new <code>uuid</code> field/column in the database</li>
|
|
<li>Iterate over the existing items in the table, to generate an uuid for them</li>
|
|
<li>Update the eventual references to the model so that it continues to work.</li>
|
|
<li>Change the old primary key to a different type</li>
|
|
<li>Mark the new primary key as such</li>
|
|
</ul>
|
|
<p>Create the new field in the 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 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 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> |