Get Primary Key column name from table in sqlalchemy (Core)

Viewed 7878

I am using the core of Sqlalchemy so I am not using a declarative base class like in other similar questions. How to get the primary key of a table using the engine?

3 Answers

I'd like to comment, but I do not have enough reputation for that.

Building on greenbergé answer:

from sqlalchemy import create_engine, Table, MetaData

dbUrl = 'postgres://localhost:5432/databaseName'   #will be different for different db's

engine = create_engine(dbUrl)
meta = MetaData()

table = Table('tableName', meta, autoload=True, autoload_with=engine)

[0] is not always the PK, only if the PK has only one column. table.primary_key.columns.values() is a list. In order to get all columns of a multi-column pk you could use:

primaryKeyColNames = [pk_column.name for pk_column in table.primary_key.columns.values()]

The two answers were given for retrieving the primary key from a metadata object.

Even if it works well, sometimes one can look for retrieving the primary key from an instance of a SQL Alchemy model, without even knowing what actually the model class is (for example if you search for having a helper function called, let's say, get_primary_key, that would accept an instance of a DB Model and output the primary keys).

For this we can use the inspect function from the inspection module :

from sqlalchemy import inspect

def get_primary_key(model_instance):
    inspector = inspect(model_instance)
    model_columns = inspector.mapper.columns
    return [c.description for c in model_columns if c.primary_key]

You could also directly use the __mapper__ object

def get_primary_key(model_instance):
    model_columns = model_instance.__mapper__.columns
    return [c.description for c in model_columns if c.primary_key]
Related