| 1 | def 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]
|
|---|