Sunday 15 June 2014

Django CharField not being inserted correctly in MySQL -


I would like to save the phone number (US) in Django through Django I import django.db import model class number (models. Model: Phone_number = models.CharField ("Phone Number", max_length = 10, Unique = true)

When I attended:

  Python manage.py sql myapp   

I found

  BEGIN; Create the tablet `Demo_Number` (` id` integer AUTO_INCREMENT is not the primary key of the tap, `phone_number` varchar (10) not unique unique);   

When I validate it, there was no error.

  Python Management and valid 0 errors found   

/ Pre>

In the MySQL console, I think:

  mysql> * Choose * from myapp_number; Empty set (0.00 seconds) mysql & gt; Description of myapp_number; + -------------- + ------------- + ------ + ----- + ------- - + ---------------- + + | Field | Type | Faucet Key | Default | Extra | + -------------- + ------------- + ------ + ----- + ------- - + ---------------- + + | ID | Int (11) | No | PRI | Faucet Auto_interpretation | | Phone_number | Varchar (10) | No | United Nations Faucet | + -------------- + ------------- + ------ + ----- + ------- - + ---------------- +   

set in 2 rows (0.03 seconds)

then Python manage.py shell , I

  demo.models import messages, numbers, relationships, SMSog N = number (phone_number = '1234567890') n.save ()   

When I check again in the MySQL console, I see:

  mysql> * Choose * from myapp_number; + ------------ + -------------- + | ID | Phone_number | + ------------ + -------------- + | 2147483647 | 1234567890 | + ------------ + -------------- + 1 line set (0.01 seconds)   

Why is < Code> ID a large number? In fact, due to this I can not insert more phone numbers anymore. For example, python manage.py shell

  n = number (phone_number = '0987654321') n.save () IntegrityError: (1062, "Duplicate entry 'Primary' for '2147483647' key))   

I'm new to Django (using Django 1.5 and MySQL Server version: 5.1.63). If someone makes me clear mistake I am appreciative of that, note one side, if I want to increase the max-length of max-length to 15, then what is the simplest and cleanest Is that Do not screw up the second set.) Thank you.

I If you do not have any data in the table, then I will try to leave the demo_number table and then syncdb will go again.

If you do not have any data in the table, then the easiest way to change the maximum length to 15 is to change the model, table DB Leave in shell, then run syncdb again. >

If you have data in the table, you can change the model, then you can update the columns in DB. In your case (MySQL specific):

  change the optional table demo_number phone_number phone_number varchar (15) not unique unique;   

For more complex migration in Django, use.

No comments:

Post a Comment