Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

error in api tutorial (is_nullable) #143

Open
wingechr opened this issue Apr 15, 2021 · 6 comments
Open

error in api tutorial (is_nullable) #143

wingechr opened this issue Apr 15, 2021 · 6 comments
Assignees
Labels
bug Something isn't working enhancement New feature or request SIROP-WP3 tutorial

Comments

@wingechr
Copy link
Contributor

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
Copy link
Contributor Author

It works if you use "is_nullable": False instead.

@jh-RLI
Copy link
Contributor

jh-RLI commented Apr 15, 2021

Good catch! should be "False" of course. I will update this in the API tutorial series.

@wingechr
Copy link
Contributor Author

Cool, thanks a lot. If you update the tutorial, I have something else that you may want to include and is currently not documented, as far as i could see:

to add (single columns) foreign keys with create table api request, you cannot use the constraints section, instead, you also add it to the column definition like so:

{
    "name": "SOME_COLUMN_NAME",
    "data_type": "SOME_DATA_TYPE",
    "foreign_key": [{
        "table": "SCHEMA.REFERENCED_TABLE",
        "column": "REFERENCED_COLUMN"
    }]
}

@jh-RLI
Copy link
Contributor

jh-RLI commented Apr 15, 2021

then we should also update the oep api readthedocs? I will include/update this in the tutorial 👍

@jh-RLI jh-RLI added bug Something isn't working enhancement New feature or request tutorial labels Apr 21, 2021
@han-f
Copy link
Contributor

han-f commented Jul 17, 2024

we can close this issue, correct? This related to some legacy tutorial @wingechr @jh-RLI?

@jh-RLI
Copy link
Contributor

jh-RLI commented Jul 17, 2024

Cool, thanks a lot. If you update the tutorial, I have something else that you may want to include and is currently not documented, as far as i could see:

to add (single columns) foreign keys with create table api request, you cannot use the constraints section, instead, you also add it to the column definition like so:

{
    "name": "SOME_COLUMN_NAME",
    "data_type": "SOME_DATA_TYPE",
    "foreign_key": [{
        "table": "SCHEMA.REFERENCED_TABLE",
        "column": "REFERENCED_COLUMN"
    }]
}

Not sure if this was updated in the "old tutorials", looking at our currently offered courses / tutorials i cant find any foreign_key references.
I think the constrains have not yet been documented for the API. I also think this may be a new issue. We would need to change the example to include a foreign_key constraint that references another table that also needs to be created in the database during the tutorial.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working enhancement New feature or request SIROP-WP3 tutorial
Projects
None yet
Development

No branches or pull requests

3 participants