4👍
The previous two answers did not help in my case, so I’m posting my solution to my case when your limit is 1000
(i.e. 1071, ‘Specified key was too long; max key length is 1000 bytes’).
First of all, make sure you are working on utf8
encoding!
Then, navigate to your setting file my.ini
, find the line default-storage-engine=xxx
. If it is
default-storage-engine=MYISAM
please change to
default-storage-engine=InnoDB
Then, the problem should be solved.
The reason is simply because MYISAM
does not support key size greater than 1000 bytes.
4👍
You can recreate “database” again:
CREATE DATABASE mydatabase CHARACTER SET utf8 DEFAULT COLLATE utf8_general_ci;
or change config file. Open /etc/mysql/mariadb.conf.d/50-server.cnf and change:
character-set-server = utf8
collation-server = utf8_general_ci
- AssertionError: The field ' ' was declared on serializer ' ', but has not been included in the 'fields' option
- Is uwsgi protocol faster than http protocol?
- Using Heroku for Django Media Files
3👍
Just upgrade and migrate database to MySQL-8.0.11 or ** MySQL-5.7.21** also make sure to use utf8 and utf8_general_ci
I had that problem, then I updated to 8.0.11 on my testing enviroment and 5.7.21 on the server. Now it migrates on both enviroments.
- Django Request Framework 'ManyRelatedField' object has no attribute 'queryset' when modifying queryset in get_fields
- Enable PK based filtering in Django Graphene Relay while retaining Global IDs
- Django – Loading Many-To-Many relationship admin page is so slow
- Saving profile with registration in Django-Registration
2👍
I think all 4 of these things are needed:
SET GLOBAL innodb_file_per_table = ON,
innodb_file_format = Barracuda,
innodb_large_prefix = ON;
CREATE/ALTER TABLE ...
ROW_FORMAT = DYNAMIC or COMPRESSED
This should get past the limit of 767 bytes for one column, but won’t get past the 3072 bytes limit for the entire index.
In order to have a compound unique index composed of strings, normalize some of the strings. Replacing a long string with a 4-byte INT will shrink the index below the limit.
- Override Django get_or_create
- Django Model Method or Calculation as Field in Database
- How can I use AWS's Dynamo Db with Django?
1👍
I ended up adding
'OPTIONS': { 'init_command': 'SET storage_engine=INNODB;' }
to my DB backed configuration in settings.py and that fixed the problem.
The MySQL server was configured to use InnoDB as a default engine, but due to some reason it still tried to create tables with the MyISAM. I am running MySQL 5.1 with Django 2.2.1 and Python 3.6.7
- South: run a migration for a column that is both unique and not null
- Django crontab not executing test function
- How to load sql fixture in Django for User model?
- Django: Customize the User model's return field
- Fabric – sudo -u
0👍
Upgrade mysql to 5.7 and try migrate again.
wget https://dev.mysql.com/get/mysql-apt-config_0.8.1-1_all.deb
sudo dpkg -i mysql-apt-config_0.8.1-1_all.deb
sudo apt-get update
sudo apt-get install mysql-server
- Django REST Framework: Validate before a delete
- Django: Possible to load fixtures with date fields based on the current date?
- Always True Q object
- Django migrate error : TypeError expected string or bytes-like object
0👍
Since this is one of the top posts for this kind of error I can share my solution that worked for me when storing S3 paths into my db.
I’m adding a MySQL prefix
index to the desired field.
# my_app/models.py
class File(models.Model):
s3_path = models.CharField(max_length=1024, help_text="S3 object key")
# my_app/migration_000x.py
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
(...),
]
operations = [
migrations.RunSQL(
sql="CREATE INDEX my_app_file_s3_path__prefix ON my_app_file (s3_path(768));",
reverse_sql="DROP INDEX my_app_file_s3_path__prefix ON my_app_file;",
)
]
I can get away with this because I know that most of the entries will be quite short (100-200 chars) so they will benefit even from just a prefix index but I still maintain max_length
that is forced upon me by AWS S3.
I’m using InnoDB
engine with utf8mb4_unicode_ci
for all my tables.
- Django: Make JS source maps compatible with staticfiles filename hashing
- For the django admin, how do I add a field to the User model and have it editable in the admin?
- Django object is not iterable using serializers.serialize
- How to set initial values for a ModelForm when instance is also given
- Django REST framework: Can a nested object access its parent object's details in a List View?
0👍
export database from local and import it using command line
like that
mysql -u username -p database_name < file.sql
this worked for me
- How to filter filter_horizontal in Django admin?
- Logging from Django under UWSGI
- Many-to-many items in a template: check if any are not empty or none
- How do you connect to Google Cloud Postgresql via Django framework?