TJB
TJB

Reputation: 3846

Unable to delete User objects in Django

I have a try except block in one of my Django view functions that will delete a User object that was created in the try block if anything should fail. When I attempt to delete the User I get this error message.

OperationalError: no such table: reversion_revision

The same issue happens in the Django admin as well. I am having issues finding similar cases of this OperationalError happening to other people, and am not sure why it is happening. All the other objects that I am deleting in the except block delete without any issues.

@csrf_exempt
def signup(request):
    # """Register a new account with a new org."""
    if request.is_ajax():
        if request.method == "POST":
            form = SignUp(requestPost(request))

        if form.is_valid():
            cleaned_data = form.cleaned_data

            email = cleaned_data['email']
            password = cleaned_data['password']
            org_name = cleaned_data['org_name']
            org_username = cleaned_data['org_username']

            if cleaned_data['token']:
                invite_token = cleaned_data['token']
            else:
                invite_token = cleaned_data['invite']

            try:
                account = Account.objects.create(email=email, password=password)
                x = email[:30]
                userExists = User.objects.filter(username=email[:30])
                if not userExists:
                    if len(email) < 30:
                        user = User.objects.create_user(email, email, password)
                    else:
                        email = email[:30]
                        user = User.objects.create_user(email, email, password)

                if invite_token:
                    if ThreadInvite.objects.filter(token=invite_token):
                        invitation = ThreadInvite.objects.get(token=invite_token)
                        thread = Thread.objects.get(id=invitation.thread.id)
                        ThreadMember.objects.create(thread=thread, account=account)
                    else:
                        invitation = OrgInvite.objects.get(token=invite_token)
                        if invitation.used:
                            raise Exception("invitation code is invalid")
                        org = Org.objects.get(id=invitation.org.id)
                        OrgMember.objects.create(org=org, account=account)
                        invitation.used = False
                        invitation.save()
                        add_to_welcome(org_id=org.id, account_id=account.id)

                if org_username and org_name:
                    org = Org.objects.create(name=org_name, username=org_username,
                                             actor=account)
                    OrgMember.objects.create(account=account, org=org)
                    Thread.objects.create(name='welcome', account=account, owner=account, org=org,
                                          purpose='To welcome new members to the team.')
                    add_to_welcome(org_id=org.id, account_id=account.id)

                login(request)

                md = mandrill.Mandrill(settings.MANDRILL_API_KEY)
                t = invite_token.replace(' ', '+')
                url = "https://localhost:8000/verify/{}".format(t)
                message = {
                    'global_merge_vars': [
                        {
                            'name': 'VERIFICATION_URL',
                            'content': url
                        },
                    ],
                    'to': [
                        {
                            'email': '[email protected]',
                        },
                    ],
                    'subject': 'Welcome to Human Link',
                }
                message['from_name'] = message.get('from_name', 'Humanlink')
                message['from_email'] = message.get('from_email',
                                                    '[email protected]')
                try:
                    md.messages.send_template(
                        template_name='humanlink-welcome', message=message,
                        template_content=[], async=True)
                except mandrill.Error as e:
                    logging.exception(e)
                    raise Exception(e)

                context = {
                    'message': 'ok'
                }

                return composeJsonResponse(200, "", context)

            except Exception, e:
                logging.error(e)
                Account.objects.filter(email=email, password=password).delete()
                User.objects.filter(username=email[:30]).delete()
                Org.objects.filter(name=org_name, username=org_username).delete()

Structure

├── account
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── admin.py
│   ├── admin.pyc
│   ├── apps.py
│   ├── forms.py
│   ├── forms.pyc
│   ├── migrations
│   ├── models.py
│   ├── models.pyc
│   ├── tests.py
│   ├── tests.pyc
│   ├── urls.py
│   ├── urls.pyc
│   ├── views.py
│   └── views.pyc
├── api_helpers.py
├── api_helpers.pyc
├── app
│   └── components
├── bower.json
├── bower_components
│   ├── angular
│   ├── angular-animate
│   ├── angular-bootstrap
│   ├── angular-cookies
│   ├── angular-messages
│   ├── angular-sanitize
│   ├── angular-scroll-glue
│   ├── angular-touch
│   ├── angular-ui-router
│   ├── bootstrap
│   ├── checklist-model
│   ├── font-awesome
│   ├── jquery
│   ├── moment
│   ├── pusher-websocket-iso
│   └── underscore
├── cron_tasks.py
├── gulpfile.js
├── logs
│   └── readme.txt
├── manage.py
├── message
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── admin.py
│   ├── admin.pyc
│   ├── forms.py
│   ├── forms.pyc
│   ├── migrations
│   ├── models.py
│   ├── models.pyc
│   ├── urls.py
│   ├── urls.pyc
│   ├── views.py
│   └── views.pyc
|
├── org
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── admin.py
│   ├── admin.pyc
│   ├── forms.py
│   ├── forms.pyc
│   ├── migrations
│   ├── models.py
│   ├── models.pyc
│   ├── tests.py
│   ├── tests.pyc
│   ├── urls.py
│   ├── urls.pyc
│   ├── views.py
│   └── views.pyc
├── package.json
├── readme.txt
├── settings
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── base.py
│   ├── base.pyc
│   ├── cron.py
│   ├── development.py
│   ├── development.pyc
│   └── production.py
├── sqlite3
│   └── local.db
├── stylesheets
│   └── less
├── templates
│   ├── accounts
│   ├── admin
│   ├── dashboard
│   ├── footer.html
│   ├── home
│   ├── layouts
│   ├── nav.html
│   ├── pages
│   ├── settings
│   ├── shared
│   ├── site-alert.html
│   └── site.html
├── third_party
│   ├── classytags
│   ├── dateutil
│   ├── django_pusher
│   ├── itsdangerous.py
│   ├── itsdangerous.pyc
│   ├── mandrill.py
│   ├── mandrill.pyc
│   ├── mptt
│   ├── pusher
│   ├── requests
│   ├── reversion
│   ├── six.py
│   ├── six.pyc
│   ├── suit
│   └── werkzeug
├── utility.pyc
├── webapp
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── static
│   ├── urls.py
│   ├── urls.pyc
│   ├── views.py
│   ├── views.pyc
│   ├── wsgi.py
│   └── wsgi.pyc
└── webapp_admin
    ├── __init__.py
    ├── __init__.pyc
    └── static

Upvotes: 1

Views: 2087

Answers (2)

Sardorbek Imomaliev
Sardorbek Imomaliev

Reputation: 15390

Problem here is not in your code but in your database state. Looks like you added django reversion app. Which adds new Models in your project. Run

python manage.py syncdb

or if you in 1.8+

python manage.py migrate

Update

If this doesn't help than there are no migrations for your 3rd-party app you need to first create them with

python manage.py makemigrations name_3rd_party_app

Be careful with creating migrations on 3rd-party apps. When you run makemigrations it creates migrations in 3rd-party app package. So it won't be in your code. And after you deploy it, or other user uses it there are will not be this migrations. So you need to supply custom path for created migrations with https://docs.djangoproject.com/en/1.9/ref/settings/#migration-modules

And then run migrate

Upvotes: 2

ss7
ss7

Reputation: 3012

Try running

./manage.py makemigrations revisions

then

./manage.py migrate

or just delete the db file and start over with

./manage.py migrate

Upvotes: 0

Related Questions