question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Error when running migration script: more than one primary key

See original GitHub issue

Steps to reproduce:

  • Add the example LdapGroup class to existing Django site
  • Run makemigrations
  • Run migrate
  • Run runserver
  • Log in as admin, visit LdapGroup entity

What should happen:

  • Migration script for LdapGroup class created
  • Migration script completes successfully
  • Login successful
  • LdapGroup contains groups on LDAP server

What happens instead:

  • Migration script for LdapGroup class created (as expected)
  • Error when running migrate (see below)
  • Cannot proceed to runserver or visit admin app
  Applying ldapregister.0003_ldapgroup...Traceback (most recent call last):
  File "/home/david/.local/share/virtualenvs/purist_web/lib/python3.5/site-packages/django/db/backends/utils.py", line 62, in execute
    return self.cursor.execute(sql)
  File "/home/david/.local/share/virtualenvs/purist_web/lib/python3.5/site-packages/django/db/backends/sqlite3/base.py", line 335, in execute
    return Database.Cursor.execute(self, query)
sqlite3.OperationalError: table "ldapregister_ldapgroup" has more than one primary key

Configuration:

settings.py (points to Online LDAP Test Server)

DATABASES = {
    'ldap': {
        'ENGINE': 'ldapdb.backends.ldap',
        'NAME': 'ldap://ldap.forumsys.com',
        'USER': 'cn=read-only-admin,dc=example,dc=com',
        'PASSWORD': 'password',
        'TLS': False,
    },
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
    },
}

DATABASE_ROUTERS = ['ldapdb.router.Router']

models.py

class LdapGroup(ldapdb.models.Model):
    """
    Class for representing an LDAP group entry.
    """

    # LDAP meta-data
    base_dn = "dc=example,dc=com"
    object_classes = ['groupOfUniqueNames']

    # posixGroup attributes
    gid = IntegerField(db_column='gidNumber', unique=True)
    name = CharField(db_column='cn', max_length=200, primary_key=True)
    members = ListField(db_column='memberUid')

    def __str__(self):
        return self.name

    def __unicode__(self):
        return self.name

admin.py

class LdapGroupAdmin(admin.ModelAdmin):
    exclude = ['dn', 'objectClass']
    list_display = ['gid', 'name']

admin.site.register(LdapGroup, LdapGroupAdmin)

0003_ldapgroup.py (autogenerated)

# -*- coding: utf-8 -*-
# Generated by Django 1.10.6 on 2017-03-10 14:24
from __future__ import unicode_literals

from django.db import migrations, models
import ldapdb.models.fields


class Migration(migrations.Migration):

    dependencies = [
        ('ldapregister', '0002_auto_20170310_1340'),
    ]

    operations = [
        migrations.CreateModel(
            name='LdapGroup',
            fields=[
                ('dn', models.CharField(max_length=200, primary_key=True, serialize=False)),
                ('gid', ldapdb.models.fields.IntegerField(db_column='gidNumber', unique=True)),
                ('name', ldapdb.models.fields.CharField(db_column='cn', max_length=200, primary_key=True, serialize=False)),
                ('members', ldapdb.models.fields.ListField(db_column='memberUid')),
            ],
            options={
                'abstract': False,
            },
        ),
    ]

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:14 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
rbarroiscommented, Mar 20, 2017

Oww.

That’s an option I’ll have to consider — maybe I could be able to bypass the migration generator and either not generate any migration code, or silently skip the migration methods.

I’ll add this to the todo-list for version 0.9.0.

1reaction
DreamerDDLcommented, Mar 22, 2019

How about adding Options.managed = false to ldapdb.Model class ?

class Meta:
    abstract = True
    managed = False

It solve problem with more than one primary key for me. Django Model Meta options

Read more comments on GitHub >

github_iconTop Results From Across the Web

Laravel 5.6: Multiple primary key defined error - Stack Overflow
It seems, if the primary key is not integer, then laravel tries to make next first integer column primary!!!
Read more >
Troubleshooting migration tasks in AWS Database Migration ...
Primary key violation errors occur when you restart a task. This error can occur when data remains in the target database from a...
Read more >
audit_events has composite primary key. Composite ... - GitLab
I would expect the database migration to not abort. Relevant logs and/or screenshots. The following is the exact error produced when running ......
Read more >
Documentation: 15: CREATE TABLE - PostgreSQL
If the same column name exists in more than one parent table, an error is reported unless the data types of the columns...
Read more >
Models - Django documentation
If you change the value of the primary key on an existing object and then ... Django will raise errors when you perform...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found