mbijou
mbijou

Reputation: 87

use column_property in sqlalchemy including integer column

I want to use column_property to get a full address including street and house number in one column. The problem is that I want to combine two columns which are of different types. House number is of type Integer and street is of nummber string that's why I get following error:

sqlalchemy.exc.ProgrammingError: (psycopg2.ProgrammingError) operator     
does not exist: text + integer
LINE 1: SELECT (address.road || ' ') + address.housenumber AS anon...

Well it works if I turn housenumber to a String column but that wouldn't be a clean way I guess.

Is there a way to cast the housenumber to a string when combining it with a column of type string into column_property?

Here is my code:

class Address(CommonColumns):
    __tablename__ = 'address'
    street = db.Column(db.String(80), nullable=False)
    housenumber = db.Column(db.Integer, nullable=False)
    fulladdress = db.column_property(street + " " + housenumber)

Upvotes: 1

Views: 2039

Answers (1)

univerio
univerio

Reputation: 20518

You need to do a cast:

fulladdress = db.column_property(street + " " + db.cast(housenumber, db.String))

Upvotes: 4

Related Questions