nooby
nooby

Reputation: 85

Getting error when trying to insert JSON field in Postgres

I have the following schema in django.

class function(models.Model):
    func_id = models.IntegerField(primary_key=True)
    func_name = models.CharField(max_length=30)
    func_args = JSONField()
    func_version = models.CharField(max_length=20)
    func_desc = models.CharField(max_length=500)
    user = models.ForeignKey(user_data,on_delete=models.CASCADE)
    container_path = models.CharField(max_length=200)
    class Meta:
        db_table = 'function'

When I am trying to run the following query in postgres shell

INSERT INTO
function(func_id,func_name,func_args,func_version,func_desc,user_id,container_path) 
VALUES (101,'Sum',{"input1":"a","input2":"b"},'1.7','blahblah',105,'/path');

I am getting below error:

ERROR:  syntax error at or near "{"
LINE 1: ...nc_desc,user_id,container_path) VALUES (101,'Sum',{"input1":...

Any clue where I am going wrong?

Upvotes: 0

Views: 1389

Answers (1)

Hugo Mota
Hugo Mota

Reputation: 11547

Try enclosing your JSON in single quotes '{...}'. Even though JSONField supposedly works with python dictionaries, when doing raw SQL, postgresql won't understand that syntax.

Upvotes: 2

Related Questions