简体   繁体   中英

How to raise exception when column name too long in Postgres + SQLalchemy?

I have a script in python that uploads files to a Postgres database server. These files are then converted to SQL tables. For this, I'm using the SQLalchemy library.

The problem arises when the column names are too long. I don't want Postgres to truncate the column names automatically when they exceed the maximum length (if I recall correctly, it's 63 in Postgres). The tables end up having columns with unintelligible names and I would just rather have the script to cancel the upload.

The obvious solution is to just "hardcode" in my script the maximum length and just raise an exception if someone tries to upload a table with "too long" column names. Nevertheless, I think that this should be configurable in SQLalchemy as, for example, it raises an exception when the table name is already in use in the database.

Extract from my script to upload table:

from SQLalchemy import (
    create_engine,
)
import pandas as pd

DB_CONFIG_DICT = {
        'user': "user",
        'host': "urlforhost.com",
        'port': 5432,
        'password': "password"
}

DB_CONN_FORMAT = "postgresql+psycopg2://{user}:{password}@{host}:{port}/{database}"
DB_CONN_URI_DEFAULT = (DB_CONN_FORMAT.format( database='sandbox', **DB_CONFIG_DICT))
engine = create_engine(DB_CONN_URI_DEFAULT)
path = "file.csv"
table_name = "table_name"
df = pd.read_csv(path, decimal=r".")
df.columns = [c.lower() for c in df.columns] #postgres doesn't like capitals or spaces
df.to_sql(table_name, engine)

I hope this can help you.

def check_column_name(name):
    if len(name) > 63:
        raise ValueError("column name (%s) is too long" % name)
df.columns = [c.lower() for c in df.columns]
map(check_column_name, df.columns)   # Check the column name before import
df.to_sql(table_name, engine)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM