SQLAlchemy warning Textual column expression should be explicitly declared?

13,776

Solution 1

You should cast it to text as the error explains. To do so, adapt the following code to your needs :)

from sqlalchemy.sql import text
...
cursor.execute(text(<whatever_needed_to_be_casted>))

Solution 2

I faced this issue and I think the problem happens when you do not provide a condition on filter(), e.g instead of declaring filter(model.Email == EmailInput) you declare filter(EmailInput)

Share:
13,776
svenema
Author by

svenema

Software engineer, adventurer, photographer, car mechanic and dragon fighter.

Updated on June 05, 2022

Comments

  • svenema
    svenema about 2 years

    I keep getting this warning and no matter what can't seem to get rid of it (besides surpressing it):

    C:\...\site-packages\sqlalchemy\sql\elements.py:4390: SAWarning:
    Textual column expression 'column_name' should be explicitly declared
    with text('column_name'), or use column('column_name') for more
    specificity 
    
    if guess_is_literal else "column"
    

    I build a list of Column() objects (column name + data type) in one metadata context, and later in another metadata context create a table using this list. While this works, it does give this warning. I've tried:

    • storing it as a "quotedname"
    • casting the column to a "ColumnClause", using column()
    • casting the column to a "TextClause", using text()
    • casting the column to a String, using str()

    No matter what, I still get the warning.

    Here are a few snippets of the Python code:

    for col_name in self.cols_source:
                print(meta.tables[self.table_name].c[col_name].name)
                print(type(meta.tables[self.table_name].c[col_name].name))          #quotedname
                print(type(column(meta.tables[self.table_name].c[col_name].name)))  #ColumnClause
                print(type(text(meta.tables[self.table_name].c[col_name].name)))    #TextClause
                print(type(str(meta.tables[self.table_name].c[col_name].name)))     #Str
    
                #source_query_cols.append( Column( name=meta.tables[self.table_name].c[col_name].name, type_=meta.tables[self.table_name].c[col_name].type ))
                #source_query_cols.append( Column( name=column(meta.tables[self.table_name].c[col_name].name), type_=meta.tables[self.table_name].c[col_name].type ))
                #source_query_cols.append( Column( name=text(meta.tables[self.table_name].c[col_name].name), type_=meta.tables[self.table_name].c[col_name].type ))
                source_query_cols.append( Column( name=str(meta.tables[self.table_name].c[col_name].name), type_=meta.tables[self.table_name].c[col_name].type ))
    
  • Juan Sánchez
    Juan Sánchez about 4 years
    You could make your answer clearer by showing the import and the text() use, that cursor part is confusing