我收到错误:错误:错误 1215:无法添加外键约束
我阅读了其他类似主题和 http://dev.mysql.com/doc/refman/5.7/en/create-table-foreign-keys.html显然我无法解决这个问题。
代码生成如下(该死的它应该可以完美运行):
SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0;
SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='TRADITIONAL,ALLOW_INVALID_DATES';
CREATE SCHEMA IF NOT EXISTS `foilmaster` DEFAULT CHARACTER SET latin1 ;
USE `foilmaster`;
-- -----------------------------------------------------
-- Table `foilmaster`.`Company`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `foilmaster`.`Company` ;
CREATE TABLE IF NOT EXISTS `foilmaster`.`Company` (
`idCompany` MEDIUMINT(9) NOT NULL AUTO_INCREMENT ,
`Name` VARCHAR(85) NOT NULL ,
`Country` VARCHAR(35) NOT NULL ,
`PotentialSaleScale` SMALLINT NOT NULL COMMENT '1-100 Where 1 is 1 Pallete a Month and 100 is 1 Truck a week' ,
`DateAdded` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ,
PRIMARY KEY (`idCompany`)
) ENGINE = InnoDB;
-- -----------------------------------------------------
-- Table `foilmaster`.`ContactData`
-- -----------------------------------------------------
DROP TABLE IF EXISTS `foilmaster`.`ContactData` ;
CREATE TABLE IF NOT EXISTS `foilmaster`.`ContactData` (
`idCompany` MEDIUMINT(9) NOT NULL ,
`Phone` VARCHAR(45) NOT NULL ,
INDEX `fk_ContactData_Company_idx` (`idCompany` ASC) ,
PRIMARY KEY (`idCompany`) ,
CONSTRAINT `fk_ContactData_Company` FOREIGN KEY (`idCompany` )
REFERENCES `foilmaster`.`Company` (`idCompany` )
ON DELETE NO ACTION
ON UPDATE NO ACTION
) ENGINE = InnoDB;
USE `foilmaster` ;
SET SQL_MODE=@OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;
InnoDB 状态显示:
=====================================
2013-05-26 13:07:32 460 INNODB MONITOR OUTPUT
=====================================
Per second averages calculated from the last 52 seconds
-----------------
BACKGROUND THREAD
-----------------
srv_master_thread loops: 8 srv_active, 0 srv_shutdown, 7450 srv_idle
srv_master_thread log flush and writes: 7458
----------
SEMAPHORES
----------
OS WAIT ARRAY INFO: reservation count 20
OS WAIT ARRAY INFO: signal count 19
Mutex spin waits 2, rounds 0, OS waits 0
RW-shared spins 19, rounds 570, OS waits 19
RW-excl spins 1, rounds 30, OS waits 1
Spin rounds per wait: 0.00 mutex, 30.00 RW-shared, 30.00 RW-excl
------------------------
LATEST FOREIGN KEY ERROR
------------------------
2013-05-26 13:07:16 f0c Error in foreign key constraint of table foilmaster/contactdata:
there is no index in referenced table which would contain
the columns as the first columns, or the data types in the
referenced table do not match the ones in table. Constraint:
,
CONSTRAINT "ID" FOREIGN KEY ("ID") REFERENCES "company" ("ID")
The index in the foreign key in table is "PRIMARY"
See http://dev.mysql.com/doc/refman/5.6/en/innodb-foreign-key-constraints.html
for correct foreign key definition.
------------
TRANSACTIONS
------------
Trx id counter 3386
Purge done for trx's n:o < 3386 undo n:o < 0 state: running but idle
History list length 240
LIST OF TRANSACTIONS FOR EACH SESSION:
---TRANSACTION 0, not started
MySQL thread id 12, OS thread handle 0x460, query id 394 localhost 127.0.0.1 root init
SHOW ENGINE INNODB STATUS
---TRANSACTION 0, not started
MySQL thread id 2, OS thread handle 0x1560, query id 83 localhost 127.0.0.1 root cleaning up
--------
FILE I/O
--------
I/O thread 0 state: wait Windows aio (insert buffer thread)
I/O thread 1 state: wait Windows aio (log thread)
I/O thread 2 state: wait Windows aio (read thread)
I/O thread 3 state: wait Windows aio (read thread)
I/O thread 4 state: wait Windows aio (read thread)
I/O thread 5 state: wait Windows aio (read thread)
I/O thread 6 state: wait Windows aio (write thread)
I/O thread 7 state: wait Windows aio (write thread)
I/O thread 8 state: wait Windows aio (write thread)
I/O thread 9 state: wait Windows aio (write thread)
Pending normal aio reads: 0 [0, 0, 0, 0] , aio writes: 0 [0, 0, 0, 0] ,
ibuf aio reads: 0, log i/o's: 0, sync i/o's: 0
Pending flushes (fsync) log: 0; buffer pool: 0
405 OS file reads, 168 OS file writes, 82 OS fsyncs
0.00 reads/s, 0 avg bytes/read, 0.37 writes/s, 0.17 fsyncs/s
-------------------------------------
INSERT BUFFER AND ADAPTIVE HASH INDEX
-------------------------------------
Ibuf: size 1, free list len 0, seg size 2, 0 merges
merged operations:
insert 0, delete mark 0, delete 0
discarded operations:
insert 0, delete mark 0, delete 0
Hash table size 58207, node heap has 0 buffer(s)
0.00 hash searches/s, 0.81 non-hash searches/s
---
LOG
---
Log sequence number 8995932
Log flushed up to 8995932
Pages flushed up to 8995932
Last checkpoint at 8995932
0 pending log writes, 0 pending chkp writes
43 log i/o's done, 0.08 log i/o's/second
----------------------
BUFFER POOL AND MEMORY
----------------------
Total memory allocated 30048256; in additional pool allocated 0
Dictionary memory allocated 55489
Buffer pool size 1792
Free buffers 1369
Database pages 423
Old database pages 0
Modified db pages 0
Pending reads 0
Pending writes: LRU 0, flush list 0 single page 0
Pages made young 0, not young 0
0.00 youngs/s, 0.00 non-youngs/s
Pages read 391, created 32, written 97
0.00 reads/s, 0.08 creates/s, 0.21 writes/s
Buffer pool hit rate 1000 / 1000, young-making rate 0 / 1000 not 0 / 1000
Pages read ahead 0.00/s, evicted without access 0.00/s, Random read ahead 0.00/s
LRU len: 423, unzip_LRU len: 0
I/O sum[0]:cur[0], unzip sum[0]:cur[0]
--------------
ROW OPERATIONS
--------------
0 queries inside InnoDB, 0 queries in queue
0 read views open inside InnoDB
Main thread id 2672, state: sleeping
Number of rows inserted 0, updated 0, deleted 0, read 0
0.00 inserts/s, 0.00 updates/s, 0.00 deletes/s, 0.00 reads/s
----------------------------
END OF INNODB MONITOR OUTPUT
============================
到目前为止已经花了 2 个小时......只是盯着看 :P
最佳答案
事实证明,这两个答案都是错误的。 解决方案是什么?
我把这一行移到了顶部
如果存在则删除表 foilmaster.ContactData ;
此外,勾选 DROP schema 复选框也有效。
我开始想知道为什么会是这样......并以通常的“只要它有效,我就很好”结束:)
关于mysql - 错误 : Error 1215: Cannot add foreign key constraint,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16758776/
大约一年前,我决定确保每个包含非唯一文本的Flash通知都将从模块中的方法中获取文本。我这样做的最初原因是为了避免一遍又一遍地输入相同的字符串。如果我想更改措辞,我可以在一个地方轻松完成,而且一遍又一遍地重复同一件事而出现拼写错误的可能性也会降低。我最终得到的是这样的:moduleMessagesdefformat_error_messages(errors)errors.map{|attribute,message|"Error:#{attribute.to_s.titleize}#{message}."}enddeferror_message_could_not_find(obje
我正在使用这个:4.times{|i|assert_not_equal("content#{i+2}".constantize,object.first_content)}我之前声明过局部变量content1content2content3content4content5我得到的错误NameError:wrongconstantnamecontent2这个错误是什么意思?我很确定我想要content2=\ 最佳答案 你必须用一个大字母来调用ruby常量:Content2而不是content2。Aconstantnamestart
我遵循MichaelHartl的“RubyonRails教程:学习Web开发”,并创建了检查用户名和电子邮件长度有效性的测试(名称最多50个字符,电子邮件最多255个字符)。test/helpers/application_helper_test.rb的内容是:require'test_helper'classApplicationHelperTest在运行bundleexecraketest时,所有测试都通过了,但我看到以下消息在最后被标记为错误:ERROR["test_full_title_helper",ApplicationHelperTest,1.820016791]test
我是rails的新手,想在form字段上应用验证。myviewsnew.html.erb.....模拟.rbclassSimulation{:in=>1..25,:message=>'Therowmustbebetween1and25'}end模拟Controller.rbclassSimulationsController我想检查模型类中row字段的整数范围,如果不在范围内则返回错误信息。我可以检查上面代码的范围,但无法返回错误消息提前致谢 最佳答案 关键是您使用的是模型表单,一种显示ActiveRecord模型实例属性的表单。c
我正在尝试编写一个将文件上传到AWS并公开该文件的Ruby脚本。我做了以下事情:s3=Aws::S3::Resource.new(credentials:Aws::Credentials.new(KEY,SECRET),region:'us-west-2')obj=s3.bucket('stg-db').object('key')obj.upload_file(filename)这似乎工作正常,除了该文件不是公开可用的,而且我无法获得它的公共(public)URL。但是当我登录到S3时,我可以正常查看我的文件。为了使其公开可用,我将最后一行更改为obj.upload_file(file
我克隆了一个rails仓库,我现在正尝试捆绑安装背景:OSXElCapitanruby2.2.3p173(2015-08-18修订版51636)[x86_64-darwin15]rails-v在您的Gemfile中列出的或native可用的任何gem源中找不到gem'pg(>=0)ruby'。运行bundleinstall以安装缺少的gem。bundleinstallFetchinggemmetadatafromhttps://rubygems.org/............Fetchingversionmetadatafromhttps://rubygems.org/...Fe
在Cooper的书BeginningRuby中,第166页有一个我无法重现的示例。classSongincludeComparableattr_accessor:lengthdef(other)@lengthother.lengthenddefinitialize(song_name,length)@song_name=song_name@length=lengthendenda=Song.new('Rockaroundtheclock',143)b=Song.new('BohemianRhapsody',544)c=Song.new('MinuteWaltz',60)a.betwee
我是Google云的新手,我正在尝试对其进行首次部署。我的第一个部署是RubyonRails项目。我基本上是在关注thisguideinthegoogleclouddocumentation.唯一的区别是我使用的是我自己的项目,而不是他们提供的“helloworld”项目。这是我的app.yaml文件runtime:customvm:trueentrypoint:bundleexecrackup-p8080-Eproductionconfig.ruresources:cpu:0.5memory_gb:1.3disk_size_gb:10当我转到我的项目目录并运行gcloudprevie
我有两个Rails模型,即Invoice和Invoice_details。一个Invoice_details属于Invoice,一个Invoice有多个Invoice_details。我无法使用accepts_nested_attributes_forinInvoice通过Invoice模型保存Invoice_details。我收到以下错误:(0.2ms)BEGIN(0.2ms)ROLLBACKCompleted422UnprocessableEntityin25ms(ActiveRecord:4.0ms)ActiveRecord::RecordInvalid(Validationfa
这个问题在这里已经有了答案:Arraysmisbehaving(1个回答)关闭6年前。是否应该这样,即我误解了,还是错误?a=Array.new(3,Array.new(3))a[1].fill('g')=>[["g","g","g"],["g","g","g"],["g","g","g"]]它不应该导致:=>[[nil,nil,nil],["g","g","g"],[nil,nil,nil]]