select * from information_schema.key_column_usage where constraint_name='[CONSTRAINT_NAME]'
Here is what the above code is Doing:
1. We’re selecting all the columns from the key_column_usage table in the information_schema database.
2. We’re filtering the results to only show rows where the constraint_name is equal to the name of the constraint we’re looking for.
The result of this query will be a table that looks like this:
TABLE_CATALOG TABLE_SCHEMA TABLE_NAME COLUMN_NAME CONSTRAINT_NAME ORDINAL_POSITION POSITION_IN_UNIQUE_CONSTRAINT
def public users id users_pkey 1 1
def public users email users_email_key 2 2
The first column is the name of the database that the table is in.
The second column is the name of the schema that the table is in.
The third column is the name of the table that the constraint is on.
The fourth column is the name of the column that the constraint is on.
The fifth column is the name of the constraint.
The sixth column is the position of the column in the constraint.
The seventh column is the position of the column in the table.
We can use this information to get the name of the column that the constraint is on.
We can do this by selecting the COLUMN_NAME column from the result of the query.
We can do this by using the following code: