Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

764
Views
After updating Enum with a new value, that value cannot be inserted with psycopg2

I want to store organization data in PostgreSQL database using Enums. Since organization names sometimes change, I need to sometimes update the Enum values, and I want to automatize this using Python with sqlalchemy and psycopg2. However after altering the Enum, new values cannot be inserted.

I suspect that this is due to internal check() method of psycopg2, since the database do accept the new value. Do you know a method to update the types known by psycopg2?

Here are my tests for this script:

class DbconTestCase(unittest.TestCase):
    def setUp(self):
        self.engine, self.meta = db.get_connection('test_user', 'test_pass', 'testdb')
        # Create test table, which will be deleted later
        self.test_table = Table('test_table', self.meta,
                                Column('id', Integer, primary_key=True),
                                Column('type', Enum('number', 'text', 'image', name='type'), nullable=False),
                                Column('text', String(32)))
        self.meta.create_all()

    def test_add_enum_value(self):
        try:
            # Add new value to enum named 'type'
            db.add_enum_value(self.engine, 'type', 'object')
        except Exception as exp:
            self.assertTrue(False, msg=exp.__cause__)
        else:
            self.assertTrue(True)

    def test_bulk_copy(self):
        types = ['number', 'text', 'image', 'object']
        objects = [{'id': idx,
                    'type': types[idx % len(types)],
                    'text': 'random text to insert'} for idx in range(10000)]
        try:
            db.bulk_copy(self.engine, str(self.test_table.name), objects)
        except Exception as exp:
            self.assertTrue(False, msg=exp.__cause__)
        else:
            self.assertTrue(True)

    def tearDown(self):
        self.meta.drop_all()

And a little explanation: Basically I create a test table in a test db, and then extend an enum type with a new value. Then I try to insert a huge amount of data, with new value of the enum among them. I got the following error:

psycopg2.DataError: invalid input value for enum type: "object"
over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

I also faced this issue, and then I solved that with update the enum value from PostgreSQL directly following this answers, we can also follow this answer to update enum on PostgreSQL directly.

over 4 years ago · Santiago Trujillo Report

0

The enum value in the database should be updated (if you dont want to use migration etc), following sql code helps you

ALTER TYPE type ADD VALUE 'object';
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!