2016-12-05 5 views
0

モデルの移行に問題があります。誰が私がなぜこれを得ているのか知っていますか?モデルをまとめて削除しましたが、TblCompaniesモデルの外部キーとして設定されたcustomer_id列を使用してモデルを再作成しようとしています。Django ForeignKey Constraint

Migrations for 'customers': 
    customers/migrations/0004_tblservicerecords.py: 
    - Create model TblServiceRecords 
(serviceenv) [email protected]:/var/www/rtservice/servicesite$ ./manage.py migrate customers 
Operations to perform: 
    Apply all migrations: customers 
Running migrations: 
    Applying customers.0004_tblservicerecords...Traceback (most recent call last): 
    File "/usr/local/lib/python3.5/dist-packages/django/db/backends/utils.py", line 64, in execute 
    return self.cursor.execute(sql, params) 
    File "/usr/local/lib/python3.5/dist-packages/django/db/backends/mysql/base.py", line 110, in execute 
    return self.cursor.execute(query, args) 
    File "/usr/local/lib/python3.5/dist-packages/pymysql/cursors.py", line 166, in execute 
    result = self._query(query) 
    File "/usr/local/lib/python3.5/dist-packages/pymysql/cursors.py", line 322, in _query 
    conn.query(q) 
    File "/usr/local/lib/python3.5/dist-packages/pymysql/connections.py", line 835, in query 
    self._affected_rows = self._read_query_result(unbuffered=unbuffered) 
    File "/usr/local/lib/python3.5/dist-packages/pymysql/connections.py", line 1019, in _read_query_result 
    result.read() 
    File "/usr/local/lib/python3.5/dist-packages/pymysql/connections.py", line 1302, in read 
    first_packet = self.connection._read_packet() 
    File "/usr/local/lib/python3.5/dist-packages/pymysql/connections.py", line 981, in _read_packet 
    packet.check_error() 
    File "/usr/local/lib/python3.5/dist-packages/pymysql/connections.py", line 393, in check_error 
    err.raise_mysql_exception(self._data) 
    File "/usr/local/lib/python3.5/dist-packages/pymysql/err.py", line 107, in raise_mysql_exception 
    raise errorclass(errno, errval) 
pymysql.err.IntegrityError: (1215, 'Cannot add foreign key constraint') 

The above exception was the direct cause of the following exception: 

Traceback (most recent call last): 
    File "./manage.py", line 16, in <module> 
    execute_from_command_line(sys.argv) 
    File "/usr/local/lib/python3.5/dist-packages/django/core/management/__init__.py", line 367, in execute_from_command_line 
    utility.execute() 
    File "/usr/local/lib/python3.5/dist-packages/django/core/management/__init__.py", line 359, in execute 
    self.fetch_command(subcommand).run_from_argv(self.argv) 
    File "/usr/local/lib/python3.5/dist-packages/django/core/management/base.py", line 294, in run_from_argv 
    self.execute(*args, **cmd_options) 
    File "/usr/local/lib/python3.5/dist-packages/django/core/management/base.py", line 345, in execute 
    output = self.handle(*args, **options) 
    File "/usr/local/lib/python3.5/dist-packages/django/core/management/commands/migrate.py", line 204, in handle 
    fake_initial=fake_initial, 
    File "/usr/local/lib/python3.5/dist-packages/django/db/migrations/executor.py", line 115, in migrate 
    state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial) 
    File "/usr/local/lib/python3.5/dist-packages/django/db/migrations/executor.py", line 145, in _migrate_all_forwards 
    state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial) 
    File "/usr/local/lib/python3.5/dist-packages/django/db/migrations/executor.py", line 244, in apply_migration 
    state = migration.apply(state, schema_editor) 
    File "/usr/local/lib/python3.5/dist-packages/django/db/backends/base/schema.py", line 92, in __exit__ 
    self.execute(sql) 
    File "/usr/local/lib/python3.5/dist-packages/django/db/backends/base/schema.py", line 112, in execute 
    cursor.execute(sql, params) 
    File "/usr/local/lib/python3.5/dist-packages/django/db/backends/utils.py", line 79, in execute 
    return super(CursorDebugWrapper, self).execute(sql, params) 
    File "/usr/local/lib/python3.5/dist-packages/django/db/backends/utils.py", line 64, in execute 
    return self.cursor.execute(sql, params) 
    File "/usr/local/lib/python3.5/dist-packages/django/db/utils.py", line 94, in __exit__ 
    six.reraise(dj_exc_type, dj_exc_value, traceback) 
    File "/usr/local/lib/python3.5/dist-packages/django/utils/six.py", line 685, in reraise 
    raise value.with_traceback(tb) 
    File "/usr/local/lib/python3.5/dist-packages/django/db/backends/utils.py", line 64, in execute 
    return self.cursor.execute(sql, params) 
    File "/usr/local/lib/python3.5/dist-packages/django/db/backends/mysql/base.py", line 110, in execute 
    return self.cursor.execute(query, args) 
    File "/usr/local/lib/python3.5/dist-packages/pymysql/cursors.py", line 166, in execute 
    result = self._query(query) 
    File "/usr/local/lib/python3.5/dist-packages/pymysql/cursors.py", line 322, in _query 
    conn.query(q) 
    File "/usr/local/lib/python3.5/dist-packages/pymysql/connections.py", line 835, in query 
    self._affected_rows = self._read_query_result(unbuffered=unbuffered) 
    File "/usr/local/lib/python3.5/dist-packages/pymysql/connections.py", line 1019, in _read_query_result 
    result.read() 
    File "/usr/local/lib/python3.5/dist-packages/pymysql/connections.py", line 1302, in read 
    first_packet = self.connection._read_packet() 
    File "/usr/local/lib/python3.5/dist-packages/pymysql/connections.py", line 981, in _read_packet 
    packet.check_error() 
    File "/usr/local/lib/python3.5/dist-packages/pymysql/connections.py", line 393, in check_error 
    err.raise_mysql_exception(self._data) 
    File "/usr/local/lib/python3.5/dist-packages/pymysql/err.py", line 107, in raise_mysql_exception 
    raise errorclass(errno, errval) 
django.db.utils.IntegrityError: (1215, 'Cannot add foreign key constraint') 

MySQLサーバを吐き出しているこの:私のmodels.pyで

2016-12-05 15:13:33 0x7f4fe812b700 Error in foreign key constraint of table cogsdb/#sql-4ed_3af6: 
FOREIGN KEY (`company_id_id`) REFERENCES `tbl Companies` (`Company ID`): 
Cannot resolve table name close to: 
(`Company ID`) 

の2つのモデルがこのです:私は削除することによってこの問題を解決することができました

class TblCompanies(models.Model): 
    company_name = models.CharField(db_column='Company Name', max_length=50) 
    billing_address = models.CharField(db_column='Billing Address', max_length=255, blank=True, null=True) 
    company_id = models.AutoField(db_column='Company ID', primary_key=True) 
    notes = models.TextField(db_column='Notes', blank=True, null=True) 
    date_entered = models.DateTimeField(db_column='Date Entered', blank=True, null=True) 
    billing_contact_id = models.IntegerField(db_column='Billing Contact ID', blank=True, null=True) 
    tech_contact_id = models.IntegerField(db_column='Tech Contact ID', blank=True, null=True) 
    archived = models.IntegerField(db_column='Archived', blank=True, null=True) 
    contact_name = models.CharField(db_column='Contact Name', max_length=50, blank=True, null=True) 
    contact_phone = models.CharField(db_column='Contact Phone', max_length=50, blank=True, null=True) 
    contact_e_mail = models.CharField(db_column='Contact e-mail', max_length=50, blank=True, null=True) 
    miles = models.FloatField(db_column='Miles', blank=True, null=True) 
    travel_time = models.FloatField(db_column='Travel Time', blank=True, null=True) 
    bill_rate = models.DecimalField(db_column='Bill Rate', max_digits=19, decimal_places=4, blank=True, null=True) 

    class Meta: 
     managed = True 
     db_table = 'tbl Companies' 
class TblServiceRecords(models.Model): 
    sr_id = models.CharField(db_column='SR ID', primary_key=True, max_length=50) # Field name made lowercase. Field renamed to remove unsuitable characters 
    company_id = models.ForeignKey('TblCompanies', on_delete=models.CASCADE) 
# company_id = models.IntegerField(db_column='Company ID', blank=True, null=True) 
    date_entered = models.DateTimeField(db_column='Date Entered', blank=True, null=True) # Field name made lowercase. Field renamed to remove unsuitable characters. 
    date_updated = models.DateTimeField(db_column='Date Updated', blank=True, null=True) # Field name made lowercase. Field renamed to remove unsuitable characters. 
    description = models.TextField(db_column='Description', blank=True, null=True) # Field name made lowercase. 
    type = models.CharField(db_column='Type', max_length=50, blank=True, null=True) # Field name made lowercase. 
    model_number = models.CharField(db_column='Model Number', max_length=50, blank=True, null=True) # Field name made lowercase. Field renamed to remove unsuitable characters. 
    horsepower = models.CharField(db_column='Horsepower', max_length=50, blank=True, null=True) # Field name made lowercase. 
    manufacturer = models.CharField(db_column='Manufacturer', max_length=50, blank=True, null=True) # Field name made lowercase. 
    volts = models.CharField(db_column='Volts', max_length=50, blank=True, null=True) # Field name made lowercase. 
    serial_number = models.CharField(db_column='Serial Number', max_length=50, blank=True, null=True) # Field name made lowercase. Field renamed to remove unsuitable characters. 
    po = models.CharField(db_column='PO', max_length=50, blank=True, null=True) # Field name made lowercase. 
    job = models.CharField(db_column='JOB', max_length=50, blank=True, null=True) # Field name made lowercase. 
    contact = models.CharField(db_column='Contact', max_length=50, blank=True, null=True) # Field name made lowercase. 
    machine_id = models.CharField(db_column='Machine ID', max_length=50, blank=True, null=True) # Field name made lowercase. Field renamed to remove unsuitable characters. 
    entered = models.DateTimeField(db_column='Entered', blank=True, null=True) # Field name made lowercase. 
    originator = models.CharField(db_column='Originator', max_length=50, blank=True, null=True) # Field name made lowercase. 
+0

手動でカラム/テーブルを削除しましたか? – lucasnadalutti

+0

いいえ、私はまずmodels.pyからモデルを削除してから、マイグレーション&マイグレーションを実行しました。それから、私はforeignkeyの変更でmodels.pyにそれを読みました。 – Flibertyjibbet

答えて

0

Company IDをインデックスから取り出し、それを外部キーとして読み込みます。

関連する問題