3

I am facing issues with "makeflag" field which is bit(1) type in my database(MySQL). I have tried using booleanField and bit1booleanfield with below syntax. But i am getting error with both. when i try POST request with json data on this model,

I get error as

"Data too long for column" on passing 1 or 0 as value.

And when i give true or false as value, then i get 400 Bad Request. Can someone please help me understand how can i post data using django and json for bit field (of mysql).

makeflag=models.BooleanField(db_column='MakeFlag', default=1)

makeflag=Bit1BooleanField()

My model is the next:

class Product(models.Model):
    productid = models.AutoField(db_column='ProductID', primary_key=True)  
    name = models.CharField(db_column='Name', max_length=50)  
    productnumber = models.CharField(db_column='ProductNumber', max_length=25)  
    makeflag = models.TextField(db_column='MakeFlag', max_length=1)
    color = models.CharField(db_column='Color', max_length=15, blank=True)
    safetystocklevel = models.SmallIntegerField(db_column='SafetyStockLevel')  
    reorderpoint = models.SmallIntegerField(db_column='ReorderPoint')  
    standardcost = models.FloatField(db_column='StandardCost') 
Lord Elrond
  • 13,430
  • 7
  • 40
  • 80
  • Please could add more info related to error that you got ? And how are you posting the data with JSON? – R. García Feb 26 '19 at 10:46
  • Hi, My sample json is as below { "name": "ramram", "productnumber":"1", "makeflag":1, "color":"Blue", "safetystocklevel":"1000", "reorderpoint":"750", "standardcost":"0"} – Divya Sharma Feb 26 '19 at 11:29
  • Why you send a string like `"1000", "750", "0"` to the safetystocklevel, reorderpoint and standardcost SmallIntegerFields. These fields must be integers no strings... – R. García Feb 26 '19 at 11:31
  • i am posting data using postman – Divya Sharma Feb 26 '19 at 11:38
  • Error : File "e:\anaconda\lib\site-packages\mysql\connector\cursor_cext.py" in execute raw_as_string=self._raw_as_string) File "e:\anaconda\lib\site-packages\mysql\connector\connection_cext.py" in cmd_query sqlstate=exc.sqlstate During handling of the above exception (Data too long for column 'MakeFlag' at row 1), another exception occurred: File "e:\anaconda\lib\site-packages\mysql\connector\cursor_cext.py" in statement return self._executed.strip().decode('utf8') During handling of the above exception ('NoneType' object has no attribute 'strip'), another exception occurred: – Divya Sharma Feb 26 '19 at 11:39
  • Could you take a print of your SQL schema in order to see what is happening at MySQL level. You only have to do the next command: `desc product;` on the MySQL client console – R. García Feb 26 '19 at 11:41
  • ProductID int(11) Name varchar(50) ProductNumber varchar(25) MakeFlag bit(1) Color varchar(15) SafetyStockLevel smallint(6) ReorderPoint smallint(6) StandardCost double – Divya Sharma Feb 26 '19 at 11:44
  • It worked with quotes when inserted manually using mysql workbench.Hence trying same with json – Divya Sharma Feb 26 '19 at 11:46
  • Maybe, do want to take a look what SQL query Django is creating... You could do this with some plugins but the fastest way is this. https://stackoverflow.com/questions/1074212/how-can-i-see-the-raw-sql-queries-django-is-running – R. García Feb 26 '19 at 11:48
  • For the other hand, could be problem of MySQL engine. https://stackoverflow.com/questions/15949038/error-code-1406-data-too-long-for-column-mysql – R. García Feb 26 '19 at 11:51

1 Answers1

1

You probably need to use django-mysql for mysql specific functionality. Have a look docs for bit here

webbyfox
  • 1,049
  • 10
  • 22