Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Interpret None as NULL in inserts #467

Description

@ixcat

In tables with NULL default values, insert behavior is inconsistent:

If a dictionary-style insert is performed using a dictionary without a key corresponding to the default-null field, the operation proceeds. In the case of a dictionary-style insert where the default-null field is present and set to None, or in the case of a list-style insert with an appropriately given positional None, the operation fails.

However, on the fetch side, records are returned with None as a present and populated value.

>>> print(OtherNull.definition)

    otherid: integer
    ---
    otherthing=NULL: varchar(255)
    
>>> OtherNull().insert1({'otherid': 1})
>>> OtherNull().insert1({'otherid': 2, 'otherthing': None})
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/Users/chris/Workspace/mlims-pipeline.ve/lib/python3.6/site-packages/datajoint/base_relation.py", line 137, in insert1
    self.insert((row,), **kwargs)
  File "/Users/chris/Workspace/mlims-pipeline.ve/lib/python3.6/site-packages/datajoint/base_relation.py", line 278, in insert
    itertools.chain.from_iterable((v for v in r['values'] if v is not None) for r in rows)))
  File "/Users/chris/Workspace/mlims-pipeline.ve/lib/python3.6/site-packages/datajoint/connection.py", line 133, in query
    cur.execute(query, args)
  File "/Users/chris/Workspace/mlims-pipeline.ve/lib/python3.6/site-packages/pymysql/cursors.py", line 163, in execute
    query = self.mogrify(query, args)
  File "/Users/chris/Workspace/mlims-pipeline.ve/lib/python3.6/site-packages/pymysql/cursors.py", line 142, in mogrify
    query = query % self._escape_args(args, conn)
TypeError: not enough arguments for format string
>>> OtherNull().insert1((2, None,))
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "/Users/chris/Workspace/mlims-pipeline.ve/lib/python3.6/site-packages/datajoint/base_relation.py", line 137, in insert1
    self.insert((row,), **kwargs)
  File "/Users/chris/Workspace/mlims-pipeline.ve/lib/python3.6/site-packages/datajoint/base_relation.py", line 278, in insert
    itertools.chain.from_iterable((v for v in r['values'] if v is not None) for r in rows)))
  File "/Users/chris/Workspace/mlims-pipeline.ve/lib/python3.6/site-packages/datajoint/connection.py", line 133, in query
    cur.execute(query, args)
  File "/Users/chris/Workspace/mlims-pipeline.ve/lib/python3.6/site-packages/pymysql/cursors.py", line 163, in execute
    query = self.mogrify(query, args)
  File "/Users/chris/Workspace/mlims-pipeline.ve/lib/python3.6/site-packages/pymysql/cursors.py", line 142, in mogrify
    query = query % self._escape_args(args, conn)
TypeError: not enough arguments for format string

on the fetch side, None is used as a substitute:

>>> OtherNull()
*otherid    otherthing    
+---------+ +------------+
1           None          
 (1 tuples)

>>> OtherNull().fetch()
array([(1, None)], dtype=[('otherid', '<i8'), ('otherthing', 'O')])
>>> OtherNull().fetch1()
OrderedDict([('otherid', 1), ('otherthing', None)])

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

enhancementIndicates new improvements

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions