2

MYSQL 中的新功能我尝试手动创建existants 表的外键,因为Django 不会修改它。

python2 manage.py sqlall <appname>

显示了我的预期,但是

python2 manage.py syncdb

没有对桌子做任何事情。以下是模型:

class A(models.Model):
  A_field1 = models.IntegerField(unique=True) # is not the primary key
  A_field2 = models.CharField(max_length=200)
  ...
  A_field3 = models.CharField(max_length=30)

# this method is to get the good name in the admin interface
def __unicode__(self):
  return self.name


class B(models.Model):
  B_field1 = models.FloatField()
  ...
  B_field2 = models.ForeignKey(A, to_field='A_field1')

但显然我操作错误,因为我在运行服务器时没有找到引用的字段,所以我尝试删除外键,我得到了这个奇怪的错误:

mysql> ALTER TABLE A DROP COLUMN A_field1;
ERROR 1025 (HY000): Error on rename of './website/#sql-41e_740' to
'./website/<tablename>' (errno: 150)

另外,我试过

mysql> ALTER TABLE B DROP COLUMN B_field2;

但是得到了与上面几乎相同的错误

有任何想法吗 ?

这里的表:

+--------------+--------------+------+-----+---------+----------------+
| Field        | Type         | Null | Key | Default | Extra          |
+--------------+--------------+------+-----+---------+----------------+
| A_field1     | int(11)      | NO   | PRI | NULL    | auto_increment |
| A_field2     | varchar(200) | NO   |     | NULL    |                |
| A_field3     | varchar(30)  | NO   |     | NULL    |                |
....   
+--------------+--------------+------+-----+---------+----------------+


+-----------------+----------+------+-----+---------+----------------+
| Field           | Type     | Null | Key | Default | Extra          |
+-----------------+----------+------+-----+---------+----------------+
| id              | int(11)  | NO   | PRI | NULL    | auto_increment |
| B_field1        | double   | NO   |     | NULL    |                |
 ...
| B_field2        | int(11)  | NO   | MUL | NULL    |                |
+-----------------+----------+------+-----+---------+----------------+
4

1 回答 1

0

您不能使用列名删除外键,因此:

SHOW CREATE TABLE myTable 

这将显示您的 KEY 的名称,然后将其删除

 ALTER TABLE myTable DROP FOREIGN KEY its_name

然后也删除列

ALTER TABLE mytable DROP COLUMN myColumn;
于 2013-08-22T10:45:40.763 回答