academy icon indicating copy to clipboard operation
academy copied to clipboard

error in api tutorial (is_nullable)

Open wingechr opened this issue 3 years ago • 6 comments

According to multiple tutorials, columns can be configured to not allow NULL values by adding "is_nullable": "NO"in the definition.

This does not work.

Here is a complete example. I create a table and upload an empty record, then retrieve the data and get a record with a NULL value (None)

import requests
import os

oep_url = 'https://openenergy-platform.org'
token = os.environ['OEP_API_TOKEN']
schema = 'model_draft'
table = 'demo_nullable'

tabl_url = oep_url + '/api/v0/schema/' + schema + '/tables/' + table + '/'

table_def = { 
    "query": { 
        "columns": [
            {"name": "id", "data_type": "bigserial", "is_nullable": "NO"},
            {"name": "name", "data_type": "int", "is_nullable": "NO"},            
        ],
        "constraints": [{"constraint_type": "PRIMARY KEY", "constraint_parameter": "id"}] 
    }
}

data = {
    "query": {} # empty record!
}

ses = requests.session()
ses.headers = {'Authorization': 'Token %s' % token}

# create table
res = ses.put(tabl_url, json=table_def)

# insert data
res = ses.post(tabl_url + 'rows/new', json=data)

# retrieve data
res = ses.get(tabl_url + 'rows')
res.json() # [{'id': 1, 'name': None}] !!! not ok !!!

# cleanup
res = ses.delete(tabl_url)

wingechr avatar Apr 15 '21 08:04 wingechr