2

I am trying to alter sequence of id field in my table using psycopg. This is works fine on my local server, but not working on production. I am not getting exceptions, the sequence is just not restarting.

def alter_sequence(last_id):
    try:
        dbname = settings.DATABASES['default']['NAME']
        user = settings.DATABASES['default']['USER']
        host = settings.DATABASES['default']['HOST']
        password = settings.DATABASES['default']['PASSWORD']
        port = settings.DATABASES['default']['PORT']
        connection = psycopg2.connect(
            dbname=dbname,
            user=user,
            password=password,
            host=host,
            port=port,
        )
        cursor = connection.cursor()
        cursor.execute('ALTER SEQUENCE "gs_requests_id_seq" RESTART WITH {}'.format(last_id))
        connection.close()
    except Exception as e:
        print(e)
    pass

I double-checked the database settings - its correct. Other database operations, performed by django ORM with this settings is works fine.

I think, this is not enough information about my project settings but dont know what information I need to specify. I have postgres 9.6 on my local computer and 10.1 on production.

2 Answers 2

1
cursor.execute('ALTER SEQUENCE gs_requests_id_seq RESTART WITH {};'.format(last_id))

Try this!! it will worked for me!

Sign up to request clarification or add additional context in comments.

Comments

1

I found source of the problem. I didn't commit:

connection.commit()

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.