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.

Issue with migrations and update to Release 3.8.0

See original GitHub issue

I updated to version 3.8.0 today, but have a problem when deploying (via Heroku). The build runs fine, but when deploying I get the following errors when migrating:

Operations to perform:
  Apply all migrations: account, admin, auth, cities_light, contenttypes, profiles, sessions, sites, socialaccount, taggit, users
Running migrations:
  Applying auth.0012_alter_user_first_name_max_length... OK
  Applying cities_light.0010_auto_20200508_1851...Traceback (most recent call last):
  File "manage.py", line 30, in <module>
    execute_from_command_line(sys.argv)
  File "/app/.heroku/python/lib/python3.8/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
    utility.execute()
  File "/app/.heroku/python/lib/python3.8/site-packages/django/core/management/__init__.py", line 395, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/app/.heroku/python/lib/python3.8/site-packages/django/core/management/base.py", line 330, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/app/.heroku/python/lib/python3.8/site-packages/django/core/management/base.py", line 371, in execute
    output = self.handle(*args, **options)
  File "/app/.heroku/python/lib/python3.8/site-packages/django/core/management/base.py", line 85, in wrapped
    res = handle_func(*args, **kwargs)
  File "/app/.heroku/python/lib/python3.8/site-packages/django/core/management/commands/migrate.py", line 243, in handle
    post_migrate_state = executor.migrate(
  File "/app/.heroku/python/lib/python3.8/site-packages/django/db/migrations/executor.py", line 117, in migrate
    state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
  File "/app/.heroku/python/lib/python3.8/site-packages/django/db/migrations/executor.py", line 147, in _migrate_all_forwards
    state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
  File "/app/.heroku/python/lib/python3.8/site-packages/django/db/migrations/executor.py", line 227, in apply_migration
    state = migration.apply(state, schema_editor)
  File "/app/.heroku/python/lib/python3.8/site-packages/django/db/migrations/migration.py", line 124, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "/app/.heroku/python/lib/python3.8/site-packages/django/db/migrations/operations/models.py", line 508, in database_forwards
    alter_together(
  File "/app/.heroku/python/lib/python3.8/site-packages/django/db/backends/base/schema.py", line 380, in alter_unique_together
    self._delete_composed_index(model, fields, {'unique': True}, self.sql_delete_unique)
  File "/app/.heroku/python/lib/python3.8/site-packages/django/db/backends/base/schema.py", line 416, in _delete_composed_index
    raise ValueError("Found wrong number (%s) of constraints for %s(%s)" % (
ValueError: Found wrong number (0) of constraints for cities_light_city(region_id, slug)

I am not sure what the source / implications of this error could be, so am raising it here as a possible issue. I’d also appreciate any suggestions on how to work around this.

Issue Analytics

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

github_iconTop GitHub Comments

1reaction
marianoeramirezcommented, Oct 8, 2020

@AndyClifton I saw your SQL before you edit the comment, and I can’t see any unique constraint in that SQL. Maybe you omit that part.

The problem is that Django is not able to found the constraints that it needs to drop before applying the new constraints. That means that somehow your table is not exactly what it expects.

Here is the SQL for the cities_light_city table from a fresh install before running the migration 0010_auto_20200508_1851.

CREATE TABLE public.cities_light_city (
	id serial NOT NULL,
	name_ascii varchar(200) NOT NULL,
	slug varchar(50) NOT NULL,
	geoname_id int4 NULL,
	alternate_names text NULL,
	"name" varchar(200) NOT NULL,
	display_name varchar(200) NOT NULL,
	search_names text NOT NULL,
	latitude numeric(8,5) NULL,
	longitude numeric(8,5) NULL,
	region_id int4 NULL,
	country_id int4 NOT NULL,
	population int8 NULL,
	feature_code varchar(10) NULL,
	timezone varchar(40) NULL,
	subregion_id int4 NULL,
	CONSTRAINT cities_light_city_geoname_id_key UNIQUE (geoname_id),
	CONSTRAINT cities_light_city_pkey PRIMARY KEY (id),
	CONSTRAINT cities_light_city_region_id_name_29b81cd4_uniq UNIQUE (region_id, name),
	CONSTRAINT cities_light_city_region_id_slug_dc18c213_uniq UNIQUE (region_id, slug)
);
-- public.cities_light_city foreign keys

ALTER TABLE public.cities_light_city ADD CONSTRAINT cities_light_city_country_id_cf310fd2_fk_cities_li FOREIGN KEY (country_id) REFERENCES cities_light_country(id) DEFERRABLE INITIALLY DEFERRED;
ALTER TABLE public.cities_light_city ADD CONSTRAINT cities_light_city_region_id_f7ab977b_fk_cities_light_region_id FOREIGN KEY (region_id) REFERENCES cities_light_region(id) DEFERRABLE INITIALLY DEFERRED;
ALTER TABLE public.cities_light_city ADD CONSTRAINT cities_light_city_subregion_id_0926d2ad_fk_cities_li FOREIGN KEY (subregion_id) REFERENCES cities_light_subregion(id) DEFERRABLE INITIALLY DEFERRED;

To solve this problem, I recommend you check what are the constraints that you have on your SQL database and to fix this you have 2 options:

  • Edit or add the constraints to match what Django is expecting
  • Drop the constraints related to unique (region_id, name) and (region_id and slug), run the following SQL, and fake the migration.
BEGIN;
--
-- Alter unique_together for city (2 constraint(s))
--
ALTER TABLE "cities_light_city" ADD CONSTRAINT "cities_light_city_region_id_subregion_id_name_cdfc77ea_uniq" UNIQUE ("region_id", "subregion_id", "name");
ALTER TABLE "cities_light_city" ADD CONSTRAINT "cities_light_city_region_id_subregion_id_slug_efb2e768_uniq" UNIQUE ("region_id", "subregion_id", "slug");
--
-- Alter field region on subregion
--
SET CONSTRAINTS "cities_light_subregi_region_id_c6e0b71f_fk_cities_li" IMMEDIATE; ALTER TABLE "cities_light_subregion" DROP CONSTRAINT "cities_light_subregi_region_id_c6e0b71f_fk_citie
s_li";
ALTER TABLE "cities_light_subregion" ALTER COLUMN "region_id" DROP NOT NULL;
ALTER TABLE "cities_light_subregion" ADD CONSTRAINT "cities_light_subregi_region_id_c6e0b71f_fk_cities_li" FOREIGN KEY ("region_id") REFERENCES "cities_light_region" ("id") DEFERRABLE
INITIALLY DEFERRED;
COMMIT;
0reactions
AndyCliftoncommented, Oct 8, 2020

Solved the problem by accidentally trashing the whole remote database. I can confirm that 3.8.0. works fine from a clean install.

It looks like this issue can be closed now. Thanks for the help!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Issue with migrations and update to Release 3.8.0 #242 - GitHub
I updated to version 3.8.0 today, but have a problem when deploying (via Heroku). The build runs fine, but when deploying I get...
Read more >
Instructions | Camunda Platform 8 Docs
These documents guide you through the process of migrating your Optimize from one Optimize minor version to the other. If you want to...
Read more >
3.8.0 release notes - django cms 3.11.0 documentation
Fixed an issue in wizards/create.html where the error message did not use the ... your database is up-to-date with migrations python manage.py cms...
Read more >
Mule Runtime 3.8.0 Release Notes - MuleSoft Documentation
x Migration to Mule 3.8.0. As a result of the unification of API Gateway Runtime with Mule Runtime 3.8.0 and several usability issues...
Read more >
Migrating from 3.8.0 - MonoGame Documentation
WindowsDX, DesktopGL, and UWP. Upgrading from 3.8.0 should be as straightforward as upgrading your TargetFramework and MonoGame version. Edit your csproj file ...
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