Asif H
Asif H

Reputation: 157

Python Django psycopg2.errors.StringDataRightTruncation: value too long for type character

I'm am the following code in my pycharm console, my date is length is clearly less than the mentioned field size, then why this error

my code

df=pd.read_csv('/home/asif/Documents/Projects/TaxVisor/legal.txt',header=None,names=['type'])


for type in df['type']:
...     print(type)
...     print(len(type))
...     LegalElement.objects.create(type=df['type']) 

My model:

class LegalElement(models.Model): type = models.CharField(max_length=25, null=False)

my data(from CSV)

PTY LTD
PTY. LTD
PTY. LIMITED
PTY. LTD.
PTY LTD.
PTY LIMITED
PROPRIETARY LTD.
PROPRIETARY LTD
PROPRIETARY LIMITED

My DB

create table company_registration_legalelement
(
    id   serial      not null
        constraint company_registration_legalelement_pkey
            primary key,
    type varchar(25) not null
);

my error:

Traceback (most recent call last):
  File "/home/asif/DevProjects/taxvisor/venv/lib/python3.8/site-packages/django/db/backends/utils.py", line 86, in _execute
    return self.cursor.execute(sql, params)
psycopg2.errors.StringDataRightTruncation: value too long for type character varying(25)

Upvotes: 1

Views: 1063

Answers (1)

Nirjal Paudel
Nirjal Paudel

Reputation: 201

The data has exceeded the max_length=25 value. Instead if using models.CharField. Please use models.TextField. It can handle any length of character body.
Doc for models.TextField

Code

Your code should be

class LegalElement(models.Model): 
        type = models.TextField(null=False)

Upvotes: 1

Related Questions