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.

SQL Errors on migrate a fresh install

See original GitHub issue

Issue summary

SQL Error when trying to migrate django.db.utils.OperationalError: near ")": syntax error

How to reproduce?

Freshly installed a wagtail project trying to isolate the issue. Followed the install instructions then tried to migrate

Technical details

  • python3, django 3.0, wagtail 2.9

Issue Analytics

  • State:open
  • Created 3 years ago
  • Comments:6

github_iconTop GitHub Comments

1reaction
jkevingutierrezcommented, Jul 15, 2020

There are a some things that I found, that could be useful:

  1. When I ran python manage.py makemigrations it generates something like this:
operations = [
       migrations.RemoveField(
            model_name='homepage',
            name='page_ptr',
        ),
        migrations.AddField(
            model_name='homepage',
            name='translatablepage_ptr',
            field=models.OneToOneField(auto_created=True, default='ptr', on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, serialize=False, to='wagtailtrans.TranslatablePage'),
            preserve_default=False,
        ),
]

The order here seems to be important, and it should be:

operations = [
      migrations.AddField(
            model_name='homepage',
            name='translatablepage_ptr',
            field=models.OneToOneField(auto_created=True, default='ptr', on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, serialize=False, to='wagtailtrans.TranslatablePage'),
            preserve_default=False,
        ),
      migrations.RemoveField(
            model_name='homepage',
            name='page_ptr',
        ),     
]
  1. After changing the order I was getting a different error:

django.db.utils.IntegrityError: NOT NULL constraint failed: new__home_homepage.page_ptr_id

But after checking the migration guide, I saw that you need to add some manual queries, like:

operations = [
        migrations.RunSQL(
            """
            BEGIN;

            -- Remove constraints so we can edit our table
            ALTER TABLE pages_homepage DROP CONSTRAINT pages_homepage_pkey CASCADE;

            -- Add ``translatablepage_ptr_id`` field and copy the ``page_ptr_id`` content
            ALTER TABLE pages_homepage ADD COLUMN translatablepage_ptr_id INTEGER UNIQUE;
            UPDATE pages_homepage SET translatablepage_ptr_id=page_ptr_id;

            -- Insert the required values in ``wagtailtrans`` table
            INSERT INTO wagtailtrans_language (code, is_default, position, live) SELECT 'en', 't', 0, 't' WHERE NOT EXISTS (SELECT code FROM wagtailtrans_language WHERE code='en');
            INSERT INTO wagtailtrans_translatablepage (translatable_page_ptr_id, canonical_page_id, language_id) SELECT translatablepage_ptr_id, NULL, 1 FROM pages_homepage;

            -- Add required indexes and constraints
            ALTER TABLE pages_homepage ADD CONSTRAINT pages_homepage_translatablepage_ptr_id_e5b77cf7_fk_wagtailtrans_translatable_page_id FOREIGN KEY (translatablepage_ptr_id) REFERENCES wagtailtrans_translatablepage (translatable_page_ptr_id) DEFERRABLE INITIALLY DEFERRED;
            ALTER TABLE pages_homepage ALTER COLUMN translatablepage_ptr_id SET NOT NULL;
            ALTER TABLE pages_homepage ADD PRIMARY KEY (translatablepage_ptr_id);

            -- Remove old page_ptr column
            ALTER TABLE pages_homepage DROP COLUMN IF EXISTS page_ptr_id;

            COMMIT;
            """,
            state_operations=[
                migrations.AddField(
                    model_name='homepage',
                    name='translatablepage_ptr',
                    field=models.OneToOneField(auto_created=True, on_delete=django.db.models.deletion.CASCADE, parent_link=True, primary_key=True, serialize=False, to='wagtailtrans.TranslatablePage'),
                    preserve_default=False,
                ),
                migrations.RemoveField(
                    model_name='homepage',
                    name='page_ptr',
                ),
            ]
        ),
    ]
  1. After replacing my migration file, I was getting another error

django.db.utils.OperationalError: cannot start a transaction within a transaction

To fix this, I removed the BEGIN; and the COMMIT; lines in the query

  1. After removing BEGIN; and COMMIT; I was getting a different error:

django.db.utils.OperationalError: near "DROP": syntax error

As per https://stackoverflow.com/a/1884893/4508187 it seems that sqlite doesn’t support alter table drop constraint

It would be good to have some documentation about how to make this work on sqlite, or if not possible, to specify that sqlite can’t be used in the example provided

0reactions
jkevingutierrezcommented, Jul 14, 2020

Hi,

I agree with @sarensabertooth, it would be good to have an easier way to implement wagtailtrans, or maybe more guidance on the documentation

I was trying to add this package to a fresh Wagtail installation, but I got that SQL error. django.db.utils.OperationalError: near ")": syntax error

If this is something complex to add to a fresh Wagtail site, I can’t imagine how it would be to add it to a complex site.

Read more comments on GitHub >

github_iconTop Results From Across the Web

SQL Server 2012 setup and migration issues - Microsoft Learn
This article describes the SQL Server 2012 setup and migration issues.
Read more >
Error after migrating the database from SQL Server 2005 to a ...
Problem; Solution. Problem. You have recently moved your database from SQL Server 2005 to a newer version, and now for some actions you...
Read more >
Laravel migration "Connection refused" [2002] | Database ...
I installed fresh laravel and edited .env. Added my MAMP host:127.0.0.1, database:"abc", username:root, password:root and it then tried run ...
Read more >
Transfer to new Server / Upgrade fails - sql error in migration ...
Once we've done all this, Omeka then tries to upgrade the installation, at which point it fails. On the new install we can...
Read more >
Cannot connect to the SQL server after migration or new ...
After migrating the SQL server to a new machine, or deploying a new SQL server, you get the following error in the Configuration...
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