在MySQL中,如何获取指向特定表的所有外键约束的列表?一个特定的专栏?这与Oracle问题相同,但对MySQL而言.
我正在尝试使用该app/console doctrine:schema:update --force命令进行架构更新,但Doctrine在以下部分失败:
An exception occurred while executing 'DROP INDEX IDX_E98F2859A074D5D7 ON contract':
SQLSTATE[HY000]: General error: 1553 Cannot drop index 'IDX_E98F2859A074D5D7': needed in a foreign key constraint
Run Code Online (Sandbox Code Playgroud)
根据另一个SO问题解决这个问题是微不足道的.该表有:
KEY `IDX_E98F2859A074D5D7` (`some_table_id`),
CONSTRAINT `FK_E98F2859A074D5D7` FOREIGN KEY (`some_table_id`) REFERENCES `some_table` (`id`)
Run Code Online (Sandbox Code Playgroud)
所以这可以通过删除匹配约束来手动解决.但有没有办法自动完成?
今天,我的任务是修复一堆迁移,以便它们可以从头开始完全前进和后退,而不依赖于从生产中的数据库转储开始。我遇到了这个。
class Migration(migrations.Migration):
dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('content', '0038_merge'),
]
operations = [
migrations.CreateModel(
name='UserRoles',
fields=[
('id', models.AutoField(verbose_name='ID', serialize=False, auto_created=True, primary_key=True)),
('role', models.CharField(max_length=100, choices=[(b'editor', b'Editor Role'), (b'read_only', b'Read Only'), (b'writer', b'Writer Role')])),
('site', models.ForeignKey(related_name='site', to='content.Site')),
('user', models.ForeignKey(related_name='user_site_roles', to=settings.AUTH_USER_MODEL)),
],
),
migrations.AlterUniqueTogether(
name='userroles',
unique_together=set([('user', 'site')]),
),
]
Run Code Online (Sandbox Code Playgroud)
向前和向后,这是它生成的 SQL。
BEGIN;
CREATE TABLE `content_userroles` (`id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY, `role` varchar(100) NOT NULL, `site_id` integer NOT NULL, `user_id` integer NOT NULL);
ALTER TABLE `content_userroles` ADD CONSTRAINT `content_userroles_user_id_798e435c65731cb9_uniq` UNIQUE (`user_id`, `site_id`); …Run Code Online (Sandbox Code Playgroud) 我想在php admin(mysql)中删除一个foriegn键,所以我在下面执行以下代码:
`ALTER TABLE Image_Question DROP INDEX FK_QuestionSession`
Run Code Online (Sandbox Code Playgroud)
问题是我收到此错误:
#1553 - Cannot drop index 'FK_QuestionSession': needed in a foreign key constraint
Run Code Online (Sandbox Code Playgroud)
QuestionId的外键从Image_Question表链接到问题表中的QuestionId.
谢谢