简体   繁体   中英

How remove double quotes from postgres table and field names on django?

I'm working on a django project with postgres where table and field names are generated with double quotes. Anyone knows how can I disable this behavior?

[Model definition]

class Test1(models.Model):
    key = models.UUIDField('key', db_column='KEY', editable=False, unique=True, default=uuid.uuid4)
    name = models.CharField('name', db_column='NAME', max_length=128, null=False)

    class Meta:
        db_table = 'Test1'

[Migtation]

operations = [
        migrations.CreateModel(
            name='Test1',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('key', models.UUIDField(db_column='KEY', default=uuid.uuid4, editable=False, unique=True, verbose_name='key')),
                ('name', models.CharField(db_column='NAME', max_length=128, verbose_name='name')),
            ],
            options={
                'db_table': 'Test1',
            },
        ),
    ]

[DDL Generated]

create table "Test1"
(
    id serial not null constraint "Teste4_pkey" primary key,
    "KEY"  uuid not null constraint "Teste4_KEY_key" unique,
    "NAME" varchar(128) not null
);

alter table "Test1" owner to postgres;

[DDL Expected]

create table Test1
(
    id serial not null constraint "Teste4_pkey" primary key,
    KEY  uuid not null constraint "Teste4_KEY_key" unique,
    NAME varchar(128) not null
);

alter table "Test1" owner to postgres;

[requirements]

  • django==2.1.10
  • django-choices==1.6.1
  • django-cors-headers==2.4.0
  • django-environ==0.4.5
  • django-extensions==2.1.4
  • djangorestframework==3.8.2
  • django-filter==2.0.0
  • drf_yasg==1.14.0
  • ruamel.yaml==0.15.100
  • drf-nested-routers==0.91
  • dj-database-url==0.5.0
  • requests==2.22
  • whitenoise==4.1.3
  • dry-rest-permissions==0.1.10
  • django-polymorphic==2.1.2
  • psycopg2==2.7.0
  • elastic-apm==4.2.2
  • brutils==1.0.1
  • Jinja2==2.10.3
  • schedule==0.6.0
  • pika==1.1.0
  • python-jose==1.4.0

This is by design in Django, and it is intentionally designed in this way. this is a parametrized way.
suppose someone has a column name with spaces like test column name then think what would happen. it will lead to some unwanted errors, so don't change the underlying logic of the framework.

Thanks @BjarniRagnarsson, The upper case letters was making this behavior of framework as @Sanjay mentioned.

Solution:

class Test1(models.Model):
    key = models.UUIDField('key', db_column='key', editable=False, unique=True, default=uuid.uuid4)
    name = models.CharField('name', db_column='name', max_length=128, null=False)

    class Meta:
        db_table = 'test1'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM