Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQLAlchemy warning Textual column expression should be explicitly declared?

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 ))
like image 505
svenema Avatar asked Feb 01 '19 16:02

svenema


People also ask

How do I select a column in SQLAlchemy?

SQLAlchemy Core The already created students table is referred which contains 4 columns, namely, first_name, last_name, course, score. But we will be only selecting a specific column. In the example, we have referred to the first_name and last_name columns. Other columns can also be provided in the entities list.

What is subquery in SQLAlchemy?

The statement ends by calling subquery() , which tells SQLAlchemy that our intention for this query is to use it inside a bigger query instead of on its own.

How do you define SQLAlchemy?

SQLAlchemy is a library that facilitates the communication between Python programs and databases. Most of the times, this library is used as an Object Relational Mapper (ORM) tool that translates Python classes to tables on relational databases and automatically converts function calls to SQL statements.

What is label in SQLAlchemy?

Label is a class within the sqlalchemy. sql. elements module of the SQLAlchemy project.


2 Answers

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>))
like image 121
sergiomafra Avatar answered Nov 15 '22 18:11

sergiomafra


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)

like image 1
Max Shady Avatar answered Nov 15 '22 20:11

Max Shady