0

I was wondering if anybody knows how to split a column in sqlalchemy to create a calculated field.

Tried:

class Pupils(db.Model):
    full_name = db.Column(db.Text) #Jonh Doe

@hybrid_property
def firstname(self):
    return self.full_name.split(" ")[0]

However, I receive the error message:

AttributeError: Neither 'InstrumentedAttribute' object nor 'Comparator' object associated with Pupils.full_name has an attribute 'split'

I was wondering what I was doing wrong? My fingers are bloody from Googling...Captain! please help!

  • You saw https://stackoverflow.com/questions/56650846/how-to-divide-two-columns-in-sqlalchemy and https://stackoverflow.com/questions/49974069/sqlalchemy-how-to-divide-2-columns-from-different-table ? – Joe Nov 08 '19 at 05:24

1 Answers1

1

Try this

from sqlalchemy import func

class Pupils(db.Model):
    full_name = db.Column(db.Text) #Jonh Doe

@hybrid_property
def firstname(self):
    return func.split_part(self.full_name, ' ', 1)
Llama.new
  • 357
  • 8
  • 23