Ticket #27090: sequence_name_getter.py

File sequence_name_getter.py, 556 bytes (added by Hanne Moa, 7 years ago)

Python-function looking up sequence_name in postgres

Line 
1def get_sequence_name(cursor, table_name, pk_name):
2    """Fetch the sequence name of an auto-incrementing field
3
4    Uses the postgres native "information_schema.columns" table,
5    resulting in a string looking like "nextval('sometable_id_seq'::regclass)"
6    """
7    query = """SELECT column_default
8        FROM information_schema.columns
9        WHERE table_name = %s AND column_name = %s;"""
10    cursor.execute(query, (table_name, pk_name))
11    result = cursor.fetchone()
12    if result is None:
13        return None
14    return result[0].split("'")[1]
Back to Top