#🔒 Query works via DBeaver, fails with psycopg2

28 messages · Page 1 of 1 (latest)

slate oceanBOT
#

@trail moth

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

lethal geyser
#

no idea what DBeaver is. Does your table indeed have a column named "date"? Might "date" be a reserved word in Postgres SQL?

trail moth
#

DBeaver is just a database tool

#

it has Date, with the D capitalized

lethal geyser
trail moth
#

But without double quotes, it should be case insensitive shouldn't it?

lethal geyser
#

If I were you, I'd choose a different name for that column

trail moth
#

Ah, shoot

#

Weird that it's only a problem when run through psycopg2, but I'll try that

#

No, same deal with a different name

#

bizarre

lethal geyser
#

so ... maybe it's time to post all your code

#

(as text, not a screenshot)

trail moth
#

One sec, need to simplify it

#
def rowCounter(conn):
    load_dotenv()
    cursor = conn.cursor()
    db_snapshot_name = os.getenv("SNAPSHOT_DB_NAME")

    query = """
    SELECT table_name
    FROM information_schema.tables
    WHERE table_schema = 'public'
    """

    cursor.execute(query)
    tables = cursor.fetchall()
    
    for table in tables:
        table_name = table[0]
        print(table_name)
        if table_name not in columnDatesToCheck():
            continue
        else:
            date_column = columnDatesToCheck()[table_name]

            cursor.execute(f"""
                SELECT DATE_TRUNC('month', {date_column}) AS month_year, COUNT(*) 
                FROM {table_name}
                WHERE DATE_TRUNC('month', {date_column}) < DATE_TRUNC('month', CURRENT_DATE)
                GROUP BY DATE_TRUNC('month', {date_column})
                ORDER BY month_year
            """)
            row_count_prod = cursor.fetchall()

            print(row_count_prod)

    cursor.close()
    conn.close()

Again, if I take that query, put it in some database tool, and run it. It doesn't care about case sensitivity

#

but here it does

#

not sure why

#
psycopg2.errors.UndefinedColumn: column "dateofservice" does not exist
LINE 2:                 SELECT DATE_TRUNC('month', dateofservice) AS...
HINT:  Perhaps you meant to reference the column "billing.DateOfService".
lethal geyser
#

I don't think that's your complete code. I want the code in order to run it, not just look at it.

trail moth
#
def columnDatesToCheck():
    """
    Dates to group by month for row counts

    :return:
    """
    return {
        "billing": "dateofservice"
        #"scheduling_data": "date",
    }
#

That's pretty much it, I can't give you the data unfortunately

lethal geyser
#

no imports?

#

just two function definitions and nothing that calls them?

#

I have zero interest in doing detective work just to figure out how to run your code.

slate oceanBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.

#

🔒 Query works via DBeaver, fails with psycopg2