Reputation: 1898
I'm doing a Django project using MySQL as the database, I have to get some data from another database to fill some fields of my Django database tables.
My Django model for student is like this:
class Student(models.Model):
#user = models.OneToOneField(User)
email = models.EmailField(blank=True, null=True)
email_verified = models.BooleanField(blank=True)
mobile = models.CharField(max_length=11, unique=True, blank=True, null=True)
mobile_verified = models.BooleanField(blank=True)
nickname = models.CharField(max_length=20, blank=True, null=True)
gender = models.PositiveSmallIntegerField(choices=GENDER_CHOICES,
null=True, blank=True)
level = models.PositiveSmallIntegerField(choices=STUDENT_LEVELS_CHOICES,
null=True, blank=True)
source = models.PositiveSmallIntegerField(choices=SOURCE_SITE, blank=True, null=True)
register_at = models.DateTimeField(blank=True, null=True)
enroll_at = models.DateTimeField(blank=True, null=True)
state = models.PositiveSmallIntegerField(choices=STUDENT_MAINTAIN_STATE, blank=True, null=True)
is_company_user = models.BooleanField(blank=True)
company_name = models.CharField(max_length=40, blank=True, null=True)
importance = models.PositiveSmallIntegerField(choices=IMPORTANCE_STATE, blank=True, null=True)
feature = models.PositiveSmallIntegerField(choices=USER_FEATURE, blank=True, null=True)
description = models.CharField(max_length=1000, blank=True, null=True)
sales = models.ForeignKey(User)
remaining = models.IntegerField(blank=True, null=True)#订单余额
last_state_change_time = models.DateTimeField(blank=True, null=True)
is_delete = models.BooleanField(blank=True, default=False)
objects = StudentManager()
def __unicode__(self):
return self.nickname
I wrote a py script to transfer data from another database,the script is:
import MySQLdb
try:
conn = MySQLdb.connect(host='localhost', user='root', passwd='', db='a')
cur = conn.cursor()
cur.execute('select * from student;')
res = cur.fetchall()
cur.close()
conn.close()
conn = MySQLdb.connect(host='localhost', user='root', passwd='', db='b')
cur = conn.cursor()
for item in res:
if item:
cur.execute('insert into student_student (email, email_verified, mobile, mobile_verified, nickname, gender, level, register_at) values (%s, %s, %s, %s, %s, %s, %s, %s);', (item[3], item[4], item[1], item[2], item[7], item[8], item[6], item[14]))
conn.commit()
cur.close()
conn.close()
except MySQLdb.Error, e:
print "Mysql Error %d: %s" % (e.args[0], e.args[1])
My goal is to read the data from "a" to "b", and some field of b could be null (because just a few fields from a is fit for b). And when I run my script, I got the following error:
Mysql Error 1452: Cannot add or update a child row: a foreign key constraint fails (crm.student_student, CONSTRAINT sales_id_refs_id_6e3649f6 FOREIGN KEY (sales_id) REFERENCES auth_user (id))
How to fix this?
I changed my storage engine to be innodb, but it doesn't work.
Upvotes: 2
Views: 7017
Reputation: 14939
sales = models.ForeignKey(User)
field is not null and yet when inserting, you're not providing any value to student_student.sales_id
. Either provide sales_id
or add blank=True
and null=True
to that field
Upvotes: 2