Django: Random ManyToMany relation is added on User connect
<p>Strangest bug I ever encountered</p>

<p>User model is the regular one from <code>conrtib.auth.User</code></p>

<p>Let's say I have the following model:</p>

<pre><code>class Region(model.Model):
    name = models.CharField(max_length=256)
    rakazim = models.ManyToManyField(settings.AUTH_USER_MODEL)
    goal = models.IntegerField(default=0)
</code></pre>

<p>and someplace else I have:</p>

<pre><code>user_model = get_user_model()
rakaz = user_model.objects.create_user(username, email, password)
</code></pre>

<p>Then Immediately after the user creation method is called the "rakaz" instance has a random region connected</p>

<pre><code>rakaz.region_set.all()  = [&lt;random_region&gt;]
</code></pre>

<p>It also sometimes connects to another model that has a similar <code>ManyToManyField</code> to <code>AUTH_USER_MODEL</code></p>

<p>I debugged with pdb into the user creation method (in auth contrib) and immediately after calling save inside this happens.</p>

<p>AFAIK it happens only on the staging server, but until I find the reason I'm afraid to deploy to prod..</p>

<p>Django version 1.84. server using mariabdb on RDS</p>

<p>I don't use signals in my code (and at all :) ) and can't find relevant third party code doing this,  (And if so it would happen on my machine also)</p>

<p>Any Ideas?</p>

<p>The issue turned out to be:
I seeded the staging server with data from prod for "region" the <code>dumpdata</code> command dumps the <code>region</code> with foreign keys for <code>rakazim</code> . But since the users where actually missing (I didn't copy the users from my prod environment) Instead of shouting at me, and not allowing me to <code>loaddata</code> with non existing foreign keys, <code>mariadb</code> chose to give me :+1 and add random foreign keys each time I created a user (Perhaps not random, but according to the imported mapping, not sure).</p>

<p>Lesson learned: use a proper DBMS and not a mysql varient.</p>

python django