1

I'm trying to use one-line property inside select query. It should be possible due to 0.7.4 & 0.7.5. Simple operation such as + (inside value_at_end property) works but I'm not able to use split() and other. Regarding 0.7.4 and 0.7.5 update it should be possible.

class Foo(db.Entity):
    _table_ = "sample_table"
    some_string_with_value_at_end = Optional(str)

    @property
    def value_at_end(self):
        return self.some_string_with_value_at_end.split('/')[-1]

    @classmethod
    def get_values_at_end(cls):
        values = select(v.value_at_end for v in cls)
        return values

items = Foo.get_values_at_end()
for each in items:
    print(each)

Getting error:

AttributeError: 'str' object has no attribute 'split': self.some_string_with_value_at_end.split (inside Foo.value_at_end)

Right now I'm using raw_sql but wanted to make more python, it should be possible?

Thanks for help!

Islam Murtazaev
  • 1,488
  • 2
  • 17
  • 27

1 Answers1

0

At this moment Pony does not know how to translate str.split method call to SQL.

Theoretically it is possible, but we need to know appropriate translation for each supported database. For some of them the resulted expression may look pretty complex as in this answer

So I doubt we will add support of str.split translation in near future. Maybe it is easier for you to use JSON column and store array of string values in it. Then you can write

class Foo(db.Entity):
    str_items = Optional(Json)

    @property
    def value_at_end(self):
        return self.str_items[-1]

    @classmethod
    def get_values_at_end(cls):
        values = select(v.value_at_end for v in cls)
        return values

Right now using negative index to access last item of JSON array should work in PostgreSQL, and I think we should add it for other databases too, it looks much easier than to add str.split method with all possible use-cases.

Alexander Kozlovsky
  • 4,739
  • 1
  • 21
  • 21