My models.py
file contains:
我的models.py文件包含:
class User(models.Model):
email = models.CharField(max_length=100, unique=True)
password = models.CharField(max_length=100)
create_time = models.DateTimeField(auto_now_add=True)
class Session(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE)
token = models.CharField(max_length=100, unique=True)
When i command python manage.py makemigrations
and then command python manage.py sqlmigrate <app_name> <migration_name>
i don't see anything that says "ON DELETE=CASCADE"
当我命令python manage.py makemigrations然后命令python manage.py sqlmigrate
However, the migrations work without failure when i type python manage.py migrate
.
但是,当我键入python manage.py migrate时,迁移工作没有失败。
Now, if i go to the mysql table (using SequelPro) and try to delete a row for a user who currently has a session entry, i get the following error: "One row was not removed. Reload the table to be sure that the contents have not changed in the meantime. Check the Console for possible errors inside the primary key(s) of this table!".
现在,如果我转到mysql表(使用SequelPro)并尝试为当前有会话条目的用户删除一行,我会收到以下错误:“未删除一行。重新加载表格以确保在此期间内容没有改变。检查控制台是否有可能在此表的主键内发生错误!“。
Now, when i go to the session table and delete the sessions of this user and then try to delete the user's row from the user table, it deletes properly. This indicates ON DELETE = CASCADE
is not actually working at the MySQL level.
现在,当我转到会话表并删除该用户的会话,然后尝试从用户表中删除用户的行时,它会正确删除。这表明ON DELETE = CASCADE实际上并不在MySQL级别工作。
How can i correct it?
我怎么能纠正它?
1 个解决方案
#1
15
From the docs (emphasis mine):
从文档(强调我的):
ForeignKey.on_delete
ForeignKey.on_delete
When an object referenced by a ForeignKey is deleted, Django will emulate the behavior of the SQL constraint specified by the on_delete argument.
当删除ForeignKey引用的对象时,Django将模拟on_delete参数指定的SQL约束的行为。
Django does not actually set an ON DELETE
clause in the database. If you need one, you can add one manually using a RunSQL
operation. Be sure to use the same index name, or keep the original index, or you might run into errors later on.
Django实际上并没有在数据库中设置ON DELETE子句。如果需要,可以使用RunSQL操作手动添加一个。请确保使用相同的索引名称,或保留原始索引,否则稍后可能会遇到错误。
#1
15
From the docs (emphasis mine):
从文档(强调我的):
ForeignKey.on_delete
ForeignKey.on_delete
When an object referenced by a ForeignKey is deleted, Django will emulate the behavior of the SQL constraint specified by the on_delete argument.
当删除ForeignKey引用的对象时,Django将模拟on_delete参数指定的SQL约束的行为。
Django does not actually set an ON DELETE
clause in the database. If you need one, you can add one manually using a RunSQL
operation. Be sure to use the same index name, or keep the original index, or you might run into errors later on.
Django实际上并没有在数据库中设置ON DELETE子句。如果需要,可以使用RunSQL操作手动添加一个。请确保使用相同的索引名称,或保留原始索引,否则稍后可能会遇到错误。