Skip to content

Commit 1eb0a0b

Browse files
committed
chore: added troubleshooting info re: collations
1 parent f59884d commit 1eb0a0b

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

README.rst

+31
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,37 @@ Notes on 1.0.0
141141
* If you have been running an earlier version you should run python manage.py update_countries_plus to update your data tables as they may contain incorrect data.
142142

143143

144+
---------------------------------------
145+
Troubleshooting
146+
---------------------------------------
147+
148+
**I get the following error when trying to run a migration adding a new ForeignKey to the Country model:**
149+
150+
::
151+
152+
django.db.utils.OperationalError: (3780, "Referencing column 'new_country_id' and referenced column 'iso' in foreign key constraint 'companies_company_new_country_id_1a75fd29_fk_countries' are incompatible.")
153+
154+
155+
Due to a decision made many years ago that cannot be easily changed now,
156+
countries_plus uses a CharField(max_length=2) for its primary key
157+
(the ``iso`` column). This means that in MySQL and probably other databases the
158+
charset and collation of the two fields (your ForeignKey and the Country.iso) field
159+
must be identical. Default collations may change over time, for example MySQL
160+
changed its default charset to ``utf8mb4`` and collation to ``utf8mb4_0900_ai_ci``
161+
in 8.0. This can cause new tables (such as ``countries_plus_country``) to be
162+
created with a different collation than older tables that were migrated to
163+
a newer version of MySQL.
164+
165+
To solve the problem, either:
166+
167+
- convert the ``countries_plus_country`` table to use the older collation than your related table is using.
168+
- or perhaps preferably, convert your related table to use the new ``utf8mb4_0900_ai_ci`` collation that the countries_plus table is using (and any other new tables in your database)
169+
170+
For example, running the following would fix the issue by converting your related table::
171+
172+
ALTER TABLE <YOUR_TABLE> CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci;
173+
174+
144175

145176
Running Tests
146177
-------------

0 commit comments

Comments
 (0)