Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insert Null value To an inet field in postgresql

I am trying to insert values to a table which contains two columns with inet types. When I try to insert a NULL value to these columns I get an error saying

ERROR: invalid input syntax for type inet: ""

Actually I am triyng to do this from python using sqlalchemy but naturally I get the same error saying:

Session.commit() error: (DataError) invalid input syntax for type inet: ""

I need to be able to add null values to these columns. These colums do not have an attribute like NOT NULL.

like image 729
Alptugay Avatar asked Apr 16 '11 07:04

Alptugay


People also ask

Can we insert null in integer column in PostgreSQL?

An integer column can be null, but '' is an empty string not null. The right syntax for a null integer (or any other sql type) is null .

How do you add null values to psycopg2?

With the current psycopg, instead of None, use a variable set to 'NULL'. If the field being inserted is a text field, using 'NULL', simply inserts those four characters. The answer above (use None ) is the correct one.

How do you handle null values in PostgreSQL?

nullif also used with the coalesce function to handle the null values. PostgreSQL nullif function returns a null value if provided expressions are equal. If two expressions provided are equal, then it provides a null value; as a result, otherwise, it will return the first expression as a result.

Does NULL value occupy space in PostgreSQL?

Answer: No, Each null value only uses one bit on disk.


1 Answers

The error message seems to indicate you are using an empty string to indicate a "null" value which is not correct.

The following should work:

INSERT INTO my_table (inet_column) VALUES (NULL);

Or if you actually mean update instead of insert:

UPDATE my_table
   SET inet_column = NULL
WHERE pk_column = 42;
like image 155
a_horse_with_no_name Avatar answered Sep 20 '22 14:09

a_horse_with_no_name