pyorient icon indicating copy to clipboard operation
pyorient copied to clipboard

OGM: NULL fields not queried correctly

Open grahamjamesaddis opened this issue 7 years ago • 0 comments

The query.py:build_wheres() function should use 'is', not '=', as an operator when the value passed is 'None'

In order to correct this issue I would do the following:

  • Extend property.py:PropertyEncoder with an 'encode_operator()' method e.g.:
    @staticmethod
    def encode_operator(value):
        if value:
            return ' = '
        else:
            return ' is '
  • Modify build_wheres() to include the call to 'encode_operator()' e.g.:
        kw_where = [u' and '.join(u'{0}{1}{2}'
            .format(PropertyEncoder.encode_name(k),
                    PropertyEncoder.encode_operator(v),
                    PropertyEncoder.encode_value(v))
                for k,v in kw_filters.items())] if kw_filters else []

The following code snippet demonstrates the issue on the following environment:

pyOrient version : 1.5.5

python version : 3.6.3

OrientDB version : 2.2.31

from pyorient.ogm import Graph, Config, declarative
from pyorient.ogm.property import (String, Integer)
ogm_config = Config.from_url("localhost/ogm_test", "root", "root", initial_drop = True)
g = Graph(ogm_config)
Node = declarative.declarative_node()

class table_a(Node):
    element_plural = 'table_a'
    column_1 = String()
    column_2 = Integer()
g.create_all(Node.registry)

db_data = [
    {"column_1":"Test 1", "column_2" : 1},
    {"column_1":"Test 1"},
    {"column_1":"Test 2", "column_2" : 1},
    {"column_1":"Test 2", "column_2" : None},
    ]

for data in db_data:
    res = g.table_a.create(**data)

query_res = g.table_a.query(**db_data[0]).all()
print(len(query_res)) # expected 1 got 1 (db_data[0])

query_res = g.table_a.query(**db_data[1]).all()
print(len(query_res)) # expected 2 got 2 (db_data[0] and db_data[1])

query_res = g.table_a.query(**db_data[2]).all()
print(len(query_res)) # expected 1 got 1 (db_data[2])

query_res = g.table_a.query(**db_data[3]).all()
print(len(query_res)) # expected 1 got 0 ??????? I expected db_data[3]

grahamjamesaddis avatar Jan 15 '18 11:01 grahamjamesaddis