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

Question:

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?

Asked By: Adrian Martinez

||

Answers:

So, I just ran into the same problem. You need to create a Table object that reflects the table for which you are looking to find the primary key.

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)

primaryKeyColName = table.primary_key.columns.values()[0].name

The Table construct above is useful for a number of different functions. I use it quite a bit for managing geospatial tables since I do not really like any of the current packages out there.

In your comment you mention that you are not defining a table…I think that means that you aren’t creating a sqlalchemy model of the the table. With the approach above, you don’t need to do that and can gather all sorts of information from a table in a more dynamic fashion. Especially useful if you are be handed someone else’s messy database!

Hope that helps.

Answered By: greenbergé

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()]
Answered By: Carlos Oliveira

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]
Answered By: jossefaz

for a reflected table this works:

insp=inspect(self.db.engine)
        pk_temp=insp.get_pk_constraint(self.__tablename__)['constrained_columns']
Answered By: Max
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.