OperationalError:外键不匹配

时间:2012-04-12 17:12:09

标签: python sqlite foreign-keys mismatch

我有两张表正在填写,'msrun'和'feature'。 'feature'有一个指向'msrun'表的'msrun_name'列的外键。插入表格工作正常。但是当我尝试从“功能”表中删除时,我收到以下错误:

pysqlite2.dbapi2.OperationalError: foreign key mismatch

根据SQLite手册中的外键规则:

- The parent table does not exist, or
- The parent key columns named in the foreign key constraint do not exist, or
- The parent key columns named in the foreign key constraint are not the primary key of the parent table and are not subject to a unique constraint using collating sequence specified in the CREATE TABLE, or
- The child table references the primary key of the parent without specifying the primary key columns and the number of primary key columns in the parent do not match the number of child key columns.

我什么也看不到我的违规行为。我的创建表格如下所示:

DROP TABLE IF EXISTS `msrun`;
-- -----------------------------------------------------
-- Table `msrun`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `msrun` (
  `msrun_name` VARCHAR(40) PRIMARY KEY NOT NULL ,
  `description` VARCHAR(500) NOT NULL );

DROP TABLE IF EXISTS `feature`;
-- -----------------------------------------------------
-- Table `feature`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `feature` (
  `feature_id` VARCHAR(40) PRIMARY KEY NOT NULL ,
  `intensity` DOUBLE NOT NULL ,
  `overallquality` DOUBLE NOT NULL ,
  `charge` INT NOT NULL ,
  `content` VARCHAR(45) NOT NULL ,
  `msrun_msrun_name` VARCHAR(40) NOT NULL ,
  CONSTRAINT `fk_feature_msrun1`
    FOREIGN KEY (`msrun_msrun_name` )
    REFERENCES `msrun` (`msrun_name` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION);

  CREATE UNIQUE INDEX `id_UNIQUE` ON `feature` (`feature_id` ASC);
  CREATE INDEX `fk_feature_msrun1` ON `feature` (`msrun_msrun_name` ASC);

据我所知,父表存在,外键指向右父键,父键是主键,外键指定主键列。

产生错误的脚本:

from pysqlite2 import dbapi2 as sqlite
import parseFeatureXML


connection = sqlite.connect('example.db')
cursor = connection.cursor()    
cursor.execute("PRAGMA foreign_keys=ON")

inputValues = ('example', 'description')
cursor.execute("INSERT INTO `msrun` VALUES(?, ?)", inputValues)
featureXML = parseFeatureXML.Reader('../example_scripts/example_files/input/featureXML_example.featureXML')

for feature in featureXML.getSimpleFeatureInfo():
    inputValues = (featureXML['id'], featureXML['intensity'],
                   featureXML['overallquality'], featureXML['charge'], 
                   featureXML['content'], 'example')
    # insert the values into msrun using ? for sql injection safety
    cursor.execute("INSERT INTO `feature` VALUES(?,?,?,?,?,?)", inputValues)
connection.commit()

for feature in featureXML.getSimpleFeatureInfo():
    cursor.execute("DELETE FROM `feature` WHERE feature_id = ?", (str(featureXML['id']),))    

编辑:

这些是具有链接到功能的外键的表。他们还没有被填补:

DROP TABLE IF EXISTS `convexhull`;
-- -----------------------------------------------------
-- Table `convexhull`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `convexhull` (
  `convexhull_id` INT PRIMARY KEY NOT NULL ,
  `mz` DOUBLE NOT NULL ,
  `rt` DOUBLE NOT NULL ,
  `feature_feature_id` VARCHAR(40) NOT NULL ,
  CONSTRAINT `fk_convexhull_feature`
    FOREIGN KEY (`feature_feature_id` )
    REFERENCES `feature` (`feature_id` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION);

  CREATE INDEX `fk_convexhull_feature` ON `convexhull` (`feature_feature_id` ASC);

DROP TABLE IF EXISTS `position`;
-- -----------------------------------------------------
-- Table `position`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `position` (
  `position_id` INT PRIMARY KEY NOT NULL ,
  `dim0` INT NOT NULL ,
  `dim1` INT NOT NULL ,
  `feature_feature_id` VARCHAR(40) NOT NULL ,
  CONSTRAINT `fk_position_feature1`
    FOREIGN KEY (`feature_feature_id` )
    REFERENCES `feature` (`feature_id` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION);

  CREATE INDEX `fk_position_feature1` ON `position` (`feature_feature_id` ASC);

DROP TABLE IF EXISTS `userParam_names`;
-- -----------------------------------------------------
-- Table `userParam_names`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `userParam_names` (
  `userParam_id` INT PRIMARY KEY NOT NULL ,
  `Name` VARCHAR(45) NOT NULL );

DROP TABLE IF EXISTS `feature_has_userParam_names`;
-- -----------------------------------------------------
-- Table IF EXISTS `feature_has_userParam_names`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `feature_has_userParam_names` (
  `feature_feature_id` VARCHAR(40) PRIMARY KEY NOT NULL ,
  `userParam_names_userParam_id` INT NOT NULL ,
  CONSTRAINT `fk_feature_has_userParam_names_feature1`
    FOREIGN KEY (`feature_feature_id` )
    REFERENCES `feature` (`feature_id` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_feature_has_userParam_names_userParam_names1`
    FOREIGN KEY (`userParam_names_userParam_id` )
    REFERENCES `userParam_names` (`userParam_id` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION);

  CREATE INDEX `fk_feature_has_userParam_names_userParam_names1` ON `feature_has_userParam_names` (`userParam_names_userParam_id` ASC);
  CREATE INDEX `fk_feature_has_userParam_names_feature1` ON `feature_has_userParam_names` (`feature_feature_id` ASC);

DROP TABLE IF EXISTS `userParam_value`;
-- -----------------------------------------------------
-- Table IF EXISTS `userParam_value`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `userParam_value` (
  `iduserParam_value` INT PRIMARY KEY NOT NULL ,
  `userParam_name` VARCHAR(45) NOT NULL ,
  `value` VARCHAR(45) NOT NULL );

DROP TABLE IF EXISTS `feature_has_userParam_names_has_userParam_value`;
-- -----------------------------------------------------
-- Table `feature_has_userParam_names_has_userParam_value`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `feature_has_userParam_names_has_userParam_value` (
  `feature_has_userParam_names_feature_feature_id` VARCHAR(40) PRIMARY KEY NOT NULL ,
  `feature_has_userParam_names_userParam_names_userParam_id` INT NOT NULL ,
  `userParam_value_iduserParam_value` INT NOT NULL ,
  CONSTRAINT `fk_feature_has_userParam_names_has_userParam_value_feature_ha1`
    FOREIGN KEY (`feature_has_userParam_names_feature_feature_id` , `feature_has_userParam_names_userParam_names_userParam_id` )
    REFERENCES `feature_has_userParam_names` (`feature_feature_id` , `userParam_names_userParam_id` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_feature_has_userParam_names_has_userParam_value_userParam_1`
    FOREIGN KEY (`userParam_value_iduserParam_value` )
    REFERENCES `userParam_value` (`iduserParam_value` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION);

  CREATE INDEX `fk_feature_has_userParam_names_has_userParam_value_userParam_1` ON  `feature_has_userParam_names_has_userParam_value` (`userParam_value_iduserParam_value` ASC);
  CREATE INDEX `fk_feature_has_userParam_names_has_userParam_value_feature_ha1` ON  `feature_has_userParam_names_has_userParam_value` (`feature_has_userParam_names_feature_feature_id` ASC, `feature_has_userParam_names_userParam_names_userParam_id` ASC);

当我从SQLite管理器执行此操作时,delete语句可以正常工作。


编辑2:

完整追溯:

Traceback (most recent call last):
  File "/homes/ndeklein/workspace/MS/Trunk/PyMS_dev/database/test.py", line 25, in <module>
    cursor.execute("DELETE FROM `feature` WHERE feature_id = 'f_13020522388175237334'")
pysqlite2.dbapi2.OperationalError: foreign key mismatch

DELETE FROM `feature` WHERE feature_id = 'f_13020522388175237334'

在SQLite Manager中工作。


编辑3

包括所有表格:

--------------------------------------------------------
-- pyMS database. Drops all tables before it makes them, should be changed before release
--------------------------------------------------------


DROP TABLE IF EXISTS `msrun`;
-- -----------------------------------------------------
-- Table `msrun`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `msrun` (
  `msrun_name` VARCHAR(40) PRIMARY KEY NOT NULL ,
  `description` VARCHAR(500) NOT NULL );


DROP TABLE IF EXISTS `feature`;
-- -----------------------------------------------------
-- Table `feature`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `feature` (
  `feature_id` VARCHAR(40) PRIMARY KEY NOT NULL ,
  `intensity` DOUBLE NOT NULL ,
  `overallquality` DOUBLE NOT NULL ,
  `charge` INT NOT NULL ,
  `content` VARCHAR(45) NOT NULL ,
  `msrun_msrun_name` VARCHAR(40) NOT NULL ,
  CONSTRAINT `fk_feature_msrun1`
    FOREIGN KEY (`msrun_msrun_name` )
    REFERENCES `msrun` (`msrun_name` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION);

  CREATE UNIQUE INDEX `id_UNIQUE` ON `feature` (`feature_id` ASC);
  CREATE INDEX `fk_feature_msrun1` ON `feature` (`msrun_msrun_name` ASC);


DROP TABLE IF EXISTS `convexhull`;
-- -----------------------------------------------------
-- Table `convexhull`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `convexhull` (
  `convexhull_id` INT PRIMARY KEY NOT NULL ,
  `mz` DOUBLE NOT NULL ,
  `rt` DOUBLE NOT NULL ,
  `feature_feature_id` VARCHAR(40) NOT NULL ,
  CONSTRAINT `fk_convexhull_feature`
    FOREIGN KEY (`feature_feature_id` )
    REFERENCES `feature` (`feature_id` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION);

  CREATE INDEX `fk_convexhull_feature` ON `convexhull` (`feature_feature_id` ASC);


DROP TABLE IF EXISTS `position`;
-- -----------------------------------------------------
-- Table `position`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `position` (
  `position_id` INT PRIMARY KEY NOT NULL ,
  `dim0` INT NOT NULL ,
  `dim1` INT NOT NULL ,
  `feature_feature_id` VARCHAR(40) NOT NULL ,
  CONSTRAINT `fk_position_feature1`
    FOREIGN KEY (`feature_feature_id` )
    REFERENCES `feature` (`feature_id` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION);

  CREATE INDEX `fk_position_feature1` ON `position` (`feature_feature_id` ASC);


DROP TABLE IF EXISTS `userParam_names`;
-- -----------------------------------------------------
-- Table `userParam_names`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `userParam_names` (
  `userParam_id` INT PRIMARY KEY NOT NULL ,
  `Name` VARCHAR(45) NOT NULL );


DROP TABLE IF EXISTS `feature_has_userParam_names`;
-- -----------------------------------------------------
-- Table IF EXISTS `feature_has_userParam_names`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `feature_has_userParam_names` (
  `feature_feature_id` VARCHAR(40) PRIMARY KEY NOT NULL ,
  `userParam_names_userParam_id` INT NOT NULL ,
  CONSTRAINT `fk_feature_has_userParam_names_feature1`
    FOREIGN KEY (`feature_feature_id` )
    REFERENCES `feature` (`feature_id` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_feature_has_userParam_names_userParam_names1`
    FOREIGN KEY (`userParam_names_userParam_id` )
    REFERENCES `userParam_names` (`userParam_id` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION);

  CREATE INDEX `fk_feature_has_userParam_names_userParam_names1` ON `feature_has_userParam_names` (`userParam_names_userParam_id` ASC);
  CREATE INDEX `fk_feature_has_userParam_names_feature1` ON `feature_has_userParam_names` (`feature_feature_id` ASC);


DROP TABLE IF EXISTS `userParam_value`;
-- -----------------------------------------------------
-- Table IF EXISTS `userParam_value`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `userParam_value` (
  `iduserParam_value` INT PRIMARY KEY NOT NULL ,
  `userParam_name` VARCHAR(45) NOT NULL ,
  `value` VARCHAR(45) NOT NULL );


DROP TABLE IF EXISTS `feature_has_userParam_names_has_userParam_value`;
-- -----------------------------------------------------
-- Table `feature_has_userParam_names_has_userParam_value`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `feature_has_userParam_names_has_userParam_value` (
  `feature_has_userParam_names_feature_feature_id` VARCHAR(40) PRIMARY KEY NOT NULL ,
  `feature_has_userParam_names_userParam_names_userParam_id` INT NOT NULL ,
  `userParam_value_iduserParam_value` INT NOT NULL ,
  CONSTRAINT `fk_feature_has_userParam_names_has_userParam_value_feature_ha1`
    FOREIGN KEY (`feature_has_userParam_names_feature_feature_id` , `feature_has_userParam_names_userParam_names_userParam_id` )
    REFERENCES `feature_has_userParam_names` (`feature_feature_id` , `userParam_names_userParam_id` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_feature_has_userParam_names_has_userParam_value_userParam_1`
    FOREIGN KEY (`userParam_value_iduserParam_value` )
    REFERENCES `userParam_value` (`iduserParam_value` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION);

  CREATE INDEX `fk_feature_has_userParam_names_has_userParam_value_userParam_1` ON  `feature_has_userParam_names_has_userParam_value` (`userParam_value_iduserParam_value` ASC);
  CREATE INDEX `fk_feature_has_userParam_names_has_userParam_value_feature_ha1` ON  `feature_has_userParam_names_has_userParam_value` (`feature_has_userParam_names_feature_feature_id` ASC, `feature_has_userParam_names_userParam_names_userParam_id` ASC);


DROP TABLE IF EXISTS `precursor`;
-- -----------------------------------------------------
-- Table `precursor`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `precursor` (
  `precursor_id` INT PRIMARY KEY NOT NULL ,
  `ion_mz` DOUBLE NOT NULL ,
  `charge_state` INT NOT NULL ,
  `peak_intensity` DOUBLE NOT NULL );


DROP TABLE IF EXISTS `spectrum`;
-- -----------------------------------------------------
-- Table `spectrum`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `spectrum` (
  `spectrum_index` INT PRIMARY KEY NOT NULL ,
  `ms_level` INT NOT NULL ,
  `base_peak_mz` DOUBLE NOT NULL ,
  `base_peak_intensity` DOUBLE NOT NULL ,
  `total_ion_current` DOUBLE NOT NULL ,
  `lowest_observes_mz` DOUBLE NOT NULL ,
  `highest_observed_mz` DOUBLE NOT NULL ,
  `scan_start_time` DOUBLE NOT NULL ,
  `ion_injection_time` DOUBLE NOT NULL ,
  `msrun_msrun_name` VARCHAR(40) NOT NULL ,
  `precursor_precursor_id` INT NOT NULL ,
  CONSTRAINT `fk_spectrum_msrun1`
    FOREIGN KEY (`msrun_msrun_name` )
    REFERENCES `msrun` (`msrun_name` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_spectrum_precursor1`
    FOREIGN KEY (`precursor_precursor_id` )
    REFERENCES `precursor` (`precursor_id` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION);

  CREATE INDEX `fk_spectrum_msrun1` ON `spectrum` (`msrun_msrun_name` ASC);
  CREATE INDEX `fk_spectrum_precursor1` ON `spectrum` (`precursor_precursor_id` ASC);


DROP TABLE IF EXISTS `spectrum_has_feature`;
-- -----------------------------------------------------
-- Table `spectrum_has_feature`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `spectrum_has_feature` (
  `spectrum_spectrum_index` INT PRIMARY KEY NOT NULL ,
  `spectrum_msrun_msrun_name` VARCHAR(40) NOT NULL ,
  `spectrum_precursor_precursor_id` INT NOT NULL ,
  `feature_feature_id` VARCHAR(40) NOT NULL ,
  `feature_msrun_msrun_name` VARCHAR(40) NOT NULL ,
  CONSTRAINT `fk_spectrum_has_feature_spectrum1`
    FOREIGN KEY (`spectrum_spectrum_index` , `spectrum_msrun_msrun_name` , `spectrum_precursor_precursor_id` )
    REFERENCES `spectrum` (`spectrum_index` , `msrun_msrun_msrun_name` , `precursor_precursor_id` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_spectrum_has_feature_feature1`
    FOREIGN KEY (`feature_feature_id` , `feature_msrun_msrun_name` )
    REFERENCES `feature` (`feature_id` , `msrun_msrun_msrun_name` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION);

  CREATE INDEX `fk_spectrum_has_feature_feature1` ON `spectrum_has_feature` (`feature_feature_id` ASC, `feature_msrun_msrun_name` ASC);
  CREATE INDEX `fk_spectrum_has_feature_spectrum1` ON `spectrum_has_feature` (`spectrum_spectrum_index` ASC, `spectrum_msrun_msrun_name` ASC, `spectrum_precursor_precursor_id` ASC);

2 个答案:

答案 0 :(得分:1)

你提到它在SQLite Manager中有效。我也无法重现,所以我想知道pysqlite2(或其sqlite版本)的版本是否存在问题,或者代码中其他地方的某些副作用。

你能发布pysqlite2及其sqlite的版本吗?我就是这样做的:

>>> from pysqlite2 import dbapi2 as sqlite
>>> sqlite.version
'2.3.5'
>>> sqlite.sqlite_version
'3.7.7.1'

<强>更新

您是否可以运行此程序,如果您仍然收到错误,请告诉我?

#!/usr/bin/python
from pysqlite2 import dbapi2 as sqlite

conn = sqlite.connect(':memory:')
cu = conn.cursor()
cu.executescript("""
DROP TABLE IF EXISTS msrun;
CREATE  TABLE IF NOT EXISTS msrun (
  msrun_name VARCHAR(40) PRIMARY KEY NOT NULL ,
  description VARCHAR(500) NOT NULL );

DROP TABLE IF EXISTS feature;
CREATE  TABLE IF NOT EXISTS feature (
  feature_id VARCHAR(40) PRIMARY KEY NOT NULL ,
  msrun_msrun_name VARCHAR(40) NOT NULL ,
  CONSTRAINT fk_feature_msrun1
    FOREIGN KEY (msrun_msrun_name )
    REFERENCES msrun (msrun_name )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION);

  CREATE UNIQUE INDEX id_UNIQUE ON feature (feature_id ASC);
  CREATE INDEX fk_feature_msrun1 ON feature (msrun_msrun_name ASC);

DROP TABLE IF EXISTS convexhull;
CREATE  TABLE IF NOT EXISTS convexhull (
  convexhull_id INT PRIMARY KEY NOT NULL ,
  mz DOUBLE NOT NULL ,
  rt DOUBLE NOT NULL ,
  feature_feature_id VARCHAR(40) NOT NULL ,
  CONSTRAINT fk_convexhull_feature
    FOREIGN KEY (feature_feature_id )
    REFERENCES feature (feature_id )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION);

  CREATE INDEX fk_convexhull_feature ON convexhull (feature_feature_id ASC);

DROP TABLE IF EXISTS position;
CREATE  TABLE IF NOT EXISTS position (
  position_id INT PRIMARY KEY NOT NULL ,
  dim0 INT NOT NULL ,
  dim1 INT NOT NULL ,
  feature_feature_id VARCHAR(40) NOT NULL ,
  CONSTRAINT fk_position_feature1
    FOREIGN KEY (feature_feature_id )
    REFERENCES feature (feature_id )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION);

  CREATE INDEX fk_position_feature1 ON position (feature_feature_id ASC);

DROP TABLE IF EXISTS userParam_names;
CREATE  TABLE IF NOT EXISTS userParam_names (
  userParam_id INT PRIMARY KEY NOT NULL ,
  Name VARCHAR(45) NOT NULL );

DROP TABLE IF EXISTS feature_has_userParam_names;
CREATE  TABLE IF NOT EXISTS feature_has_userParam_names (
  feature_feature_id VARCHAR(40) PRIMARY KEY NOT NULL ,
  userParam_names_userParam_id INT NOT NULL ,
  CONSTRAINT fk_feature_has_userParam_names_feature1
    FOREIGN KEY (feature_feature_id )
    REFERENCES feature (feature_id )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT fk_feature_has_userParam_names_userParam_names1
    FOREIGN KEY (userParam_names_userParam_id )
    REFERENCES userParam_names (userParam_id )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION);

  CREATE INDEX fk_feature_has_userParam_names_userParam_names1 ON feature_has_userParam_names (userParam_names_userParam_id ASC);
  CREATE INDEX fk_feature_has_userParam_names_feature1 ON feature_has_userParam_names (feature_feature_id ASC);

DROP TABLE IF EXISTS userParam_value;
CREATE  TABLE IF NOT EXISTS userParam_value (
  iduserParam_value INT PRIMARY KEY NOT NULL ,
  userParam_name VARCHAR(45) NOT NULL ,
  value VARCHAR(45) NOT NULL );

DROP TABLE IF EXISTS feature_has_userParam_names_has_userParam_value;
CREATE  TABLE IF NOT EXISTS feature_has_userParam_names_has_userParam_value (
  feature_has_userParam_names_feature_feature_id VARCHAR(40) PRIMARY KEY NOT NULL ,
  feature_has_userParam_names_userParam_names_userParam_id INT NOT NULL ,
  userParam_value_iduserParam_value INT NOT NULL ,
  CONSTRAINT fk_feature_has_userParam_names_has_userParam_value_feature_ha1
    FOREIGN KEY (feature_has_userParam_names_feature_feature_id , feature_has_userParam_names_userParam_names_userParam_id )
    REFERENCES feature_has_userParam_names (feature_feature_id , userParam_names_userParam_id )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT fk_feature_has_userParam_names_has_userParam_value_userParam_1
    FOREIGN KEY (userParam_value_iduserParam_value )
    REFERENCES userParam_value (iduserParam_value )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION);

  CREATE INDEX fk_feature_has_userParam_names_has_userParam_value_userParam_1 ON  feature_has_userParam_names_has_userParam_value (userParam_value_iduserParam_value ASC);
  CREATE INDEX fk_feature_has_userParam_names_has_userParam_value_feature_ha1 ON  feature_has_userParam_names_has_userParam_value (feature_has_userParam_names_feature_feature_id ASC, feature_has_userParam_names_userParam_names_userParam_id ASC);
""")

conn.commit()
cu.execute("PRAGMA foreign_keys=ON")
cu.execute("INSERT INTO msrun VALUES (?,?)", ('bar', 'bar'))
cu.execute("INSERT INTO feature VALUES (?,?)", ('foo','bar'))
cu.execute("INSERT INTO feature VALUES (?,?)", ('foo1','bar'))
conn.commit()

cu.execute("SELECT * FROM feature")
print(cu.fetchall())

cu.execute("DELETE FROM feature WHERE feature_id=?", ("foo", ))
cu.execute("DELETE FROM feature WHERE feature_id=?", (str("foo1"), ))
conn.commit()

cu.execute("SELECT * FROM feature")
print(cu.fetchall())

更新2

您没有收到任何错误这一事实应该排除features表及其依赖关系的问题,这与您发布的架构有关。根据sqlite文档,我现在假设你有一些其他外键来自/ {你没有包含的features表的后代/上升(例如你没有提到的某些表取决于某些表你提到这取决于features)。

sqlite doc on foreign keys的一部分让我相信:

  

如果数据库架构包含需要的外键错误   查看多个表定义以识别,然后查找   创建表时未检测到错误。相反,这样   错误阻止应用程序准备SQL语句   以使用方式修改子表或父表的内容   外键。内容更改时报告的错误是“DML错误”   更改架构时报告的错误是“DDL​​错误”。所以,   换句话说,错误配置的外键约束需要   看着孩子和父母都是DML错误。英语   外键DML错误的语言错误消息通常是“外来的   密钥不匹配“但如果父表格也可以是”没有这样的表“   不存在。

如果你没有注意到,你得到的错误与关于FK违规的错误不同:

pysqlite2.dbapi2.IntegrityError: foreign key constraint failed

更新3

您的问题在于spectrum_has_feature的FK引用了不存在的feature字段:

CREATE  TABLE IF NOT EXISTS spectrum_has_feature (
<...>
FOREIGN KEY (feature_feature_id , feature_msrun_msrun_name )
REFERENCES feature (
    feature_id , 
    msrun_msrun_msrun_name ) -- <<< shouldn't this be msrun_msrun_name?
<...>

答案 1 :(得分:0)

我在更新到Django==2.0后登陆这里。 我所需要的修复只是删除我使用Django==1.11.6编写的所有迁移,并使用makemigrations运行新的2.0。 当然,如果我有任何数据库托管我的应用程序需要维护迁移文件

,我就不能这样做