2013-05-09 2 views
1

내가 데이터베이스로 MySQL을 사용하여 장고 프로젝트를하고 있어요 실패, 내 장고 데이터베이스 테이블의 일부 필드를 채우기 위해 다른 데이터베이스에서 일부 데이터를 얻을 수있는외래 키 제약 조건이

학생을위한 내 장고 모델이 같다

:

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]) 

내 목표 "은"을 "B"에서 데이터를 읽는 것입니다, 일부 필드 :

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 

내가 다른 데이터베이스에서 데이터를 전송하는 평 스크립트를 작성, 스크립트입니다 의 b는 n 일 수있다. ull (a에서 몇 개의 필드 만 b에 적합하도록합니다).

MySQL의 오류 1452 : 추가 또는 자식 행을 업데이트 할 수 없습니다 : 외래 키 제약 조건이 실패 (crm.student_student, CONSTRAINT sales_id_refs_id_6e3649f6 FOREIGN KEY (sales_id) 참조 AUTH_USER (ID 내 스크립트를 실행할 때, 나는 다음과 같은 오류가 발생했습니다))

이 문제를 해결하는 방법은 무엇입니까?

스토리지 엔진을 innodb로 변경했으나 작동하지 않습니다. 제 질문에 답하지 마십시오. 정말 도움이 필요합니다.

+1

질문을 downvote하지 않도록 요청하지 마십시오. downvoted 얻지 않는 방법은 좋은 품질 질문을 요구하기위한 것이다. –

+0

의견을 보내 주셔서 감사합니다. 저는 웹 개발의 초보자입니다. 제 질문을 분명히하기 위해 최선을 다 했었습니다. 사람들이 제 질문을 왜 내버려 두지 않고, 왜 똑같이 만들지 않기를 바랍니다. 실수 두 번. –

답변

1

sales = models.ForeignKey(User) 필드가 null이 아니며 삽입 할 때 student_student.sales_id에 값을 제공하지 않습니다. sales_id을 입력하거나 blank=Truenull=True을 해당 필드에 추가하십시오.

+0

대단히 감사합니다 :) –