Django ForeignKey约束

时间:2016-12-05 20:10:15

标签: mysql sql django django-admin

我在迁移模型更改时遇到问题。有谁知道我为什么得到这个?我已经将模型全部删除了,现在我正在尝试使用customer_id列设置为TblCompanies模型的外键来重新创建它。

Migrations for 'customers':
  customers/migrations/0004_tblservicerecords.py:
    - Create model TblServiceRecords
(serviceenv) addohm@LevenServer:/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服务器正在吐出这个:

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

我的models.py中的两个模型是:

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.

1 个答案:

答案 0 :(得分:0)

我能够通过从索引中删除Company ID然后将其作为外键读取来解决此问题。