0

I have simple model following.

from django.db import models
class ItemAttr(models.model):
    name = models.CharField(max_length=255)
    value = models.CharField(max_length=255)

converted to

CREATE TABLE `webapp_itemattr` (
    `id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY,
    `name` varchar(255) NOT NULL,
    `value` varchar(255) NOT NULL
)

Now, I want to distinguish between uppercase and lowercase in "name" field. How do this? My goal is to get to following SQL.

CREATE TABLE `webapp_itemattr` (
    `id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY,
    `name` varchar(255) BINARY NOT NULL,
    `value` varchar(255) NOT NULL
)

apply ALTER TABLE SQL directly, it is work. but at django unit test, this MODIFY SQL is not applyed.

ALTER TABLE webapp_itemattr MODIFY name VARCHAR(255) BINARY NOT NULL;

1 Answer 1

1

As a workaround you can inherit from CharField and create you own field:

class CustomBinaryCharField(models.CharField):
    def db_type(self, connection):
        return super(CustomBinaryCharField, self).db_type(connection) + ' binary'

Here is a link to Django doc about custom fields.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.