6

I have a model (declared using Declarative base) called DevicesGpsTelemetry. I make query like this:

models = session.query(
    DevicesGps.ReceivedDateUtc, 
    DevicesGps.ReceivedTimeUtc,   
    DevicesGps.Latitude,                                 
    DevicesGps.Longitude)

And it renders as:

    SELECT   
    devices_gps."ReceivedDateUtc" AS "devices_gps_ReceivedDateUtc", 
    devices_gps."ReceivedTimeUtc" AS "devices_gps_ReceivedTimeUtc", 
    devices_gps."Latitude" AS "devices_gps_Latitude", 
    devices_gps."Longitude" AS "devices_gps_Longitude" 
        FROM devices_gps

My question: how to change the names which go after AS statement (like "gps_telemetry_ReceivedDateUtc") to something I want?

Background: these names are important for me because I do pandas.read_sql with this query and the names become DataFrame's column names

1 Answer 1

7

Add .label('desired_name') after each column. In your case it would look like

models = session.query(
    DevicesGps.ReceivedDateUtc.label("gps_telemetry_ReceivedDateUtc"), 
    DevicesGps.ReceivedTimeUtc.label("gps_telemetry_ReceivedTimeUtc"),   
    DevicesGps.Latitude.label("gps_telemetry_Latitude"),                                 
    DevicesGps.Longitude.label("gps_telemetry_Longitude")
)
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. Somehow missed this in documentation

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.