Django creating wrong type for fields in intermediate table (manytomany)
<p>I have a model in Django which the <code>pk</code> is not an integer and it has a field which is a <code>manytomany</code>. This <code>manytomany</code> references the model itself.</p>

<p>When I ran <code>makemigration</code> I didn't realize, but it did not create the fields in the intermediate table as <code>char(N)</code>. In fact, it create as an <code>integer</code>. </p>

<pre><code># models.py
class Inventory(models.Model):
    sample_id = models.CharField(max_length=50, primary_key=True)
    parent_id = models.ManyToManyField("self")
</code></pre>

<p>This throws errors whenever I try to add objects to my parent model</p>

<pre><code>&gt;&gt;&gt; p = Inventory.objects.get(sample_id='sample01')
&gt;&gt;&gt; child = Inventory.objects.get(sample_id='sample02')
&gt;&gt;&gt; p.parent_id.add(child)
</code></pre>

<p>I get the error</p>

<pre><code>psycopg2.DataError: invalid input syntax for integer: "sample02"
LINE 1: ...HERE ("inventory_parent_id"."to_inventory_id" IN ('sample...
</code></pre>

<p>I saw the fields in the intermediate table, <code>inventory_parent_id</code>, created by Django and their types are not correct.</p>

<pre><code>Columns (3)
|--id (integer)
|--from_inventory_id (integer)
|--to_inventory_id (integer)
</code></pre>

<p><strong>My questions are</strong>: Is it bad if I change the types manually? Will it break the migrations? Or did I have to do something so Django can catch this misleading type?</p>

<p>Try to re-create migrations (unapply migrations by using <code>./manage.py migrate YOURAPP PREVIOUS_MIGRATION_NUMBER</code> or <code>./manage.py migrate YOURAPP zero</code> if it's initial migration), remove migration file (don't forget about <code>.pyc</code> file) and generate it again.</p>

<p>If that doesn't help, you can try to create custom through table with proper migration fields and then recreate that migration.</p>

python django django-models manytomanyfield