草庐IT

mysql - 不允许添加外键

coder 2023-10-23 原文

我正在使用mysql

我想添加外键,但出现错误

Error Code: 1215. Cannot add foreign key constraint

我正在使用的查询是

ALTER TABLE person ADD CONSTRAINT person_company_FK FOREIGN KEY (company_id) REFERENCES company(conpany_id) ;

公司的建表语句是

CREATE TABLE `company` (
  `Company_id` int(11) NOT NULL AUTO_INCREMENT,
  `Company_name` varchar(100) CHARACTER SET utf8 DEFAULT NULL,
  `Address_id` int(11) NOT NULL DEFAULT '0',
  `Employee_range_id` int(11) NOT NULL DEFAULT '0',
  `Location_type_id` int(11) NOT NULL DEFAULT '0',
  `Ownership_id` int(11) NOT NULL DEFAULT '0',
  `Annual_sales_id` int(11) NOT NULL DEFAULT '0',
  `Sic_id` int(11) NOT NULL DEFAULT '0',
  PRIMARY KEY (`Company_id`)
) ENGINE=InnoDB AUTO_INCREMENT=131135 DEFAULT CHARSET=latin1

为人创建表是

CREATE TABLE `person` (
  `person_id` int(11) NOT NULL AUTO_INCREMENT,
  `FirstName` varchar(255) DEFAULT NULL,
  `MiddleName` varchar(255) DEFAULT NULL,
  `LastName` varchar(255) DEFAULT NULL,
  `Suffix` varchar(255) DEFAULT NULL,
  `Number_YearsAnAgent` double DEFAULT NULL,
  `BD_RIARep` varchar(255) DEFAULT NULL,
  `RepCRD` double DEFAULT NULL,
  `BrokerDealerAffiliation` varchar(255) DEFAULT NULL,
  `RIAAffiliation` varchar(255) DEFAULT NULL,
  `AgentLicenseType_Health` varchar(255) DEFAULT NULL,
  `Number_StateLicenses_Health` double DEFAULT NULL,
  `AgentLicenseType_Life` varchar(255) DEFAULT NULL,
  `Number_StateLicenses_Life` double DEFAULT NULL,
  `AgentLicenseType_PropertyCasualty` varchar(255) DEFAULT NULL,
  `Number_StateLicenses_PropertyCasualty` double DEFAULT NULL,
  `AgentLicenseType_VariableProducts` varchar(255) DEFAULT NULL,
  `Number_StateLicenses_VariableProducts` double DEFAULT NULL,
  `SellsRetirementPlanProducts` varchar(255) DEFAULT NULL,
  `DBAName` varchar(255) DEFAULT NULL,
  `DateOfBirth_full` datetime DEFAULT NULL,
  `Gender` varchar(255) DEFAULT NULL,
  `office_id` int(11) NOT NULL,
  `company_id` int(11) NOT NULL DEFAULT '1',
  PRIMARY KEY (`person_id`),
  KEY `person_Office_FK` (`office_id`),
  CONSTRAINT `person_Office_FK` FOREIGN KEY (`office_id`) REFERENCES `office` (`Office_id`)
) ENGINE=InnoDB AUTO_INCREMENT=2890997 DEFAULT CHARSET=latin1

我使用 SHOW ENGINE innodb STATUS 来检查发生了什么,但不明白响应是什么

=====================================
2014-08-08 09:26:17 1754 INNODB MONITOR OUTPUT
=====================================
Per second averages calculated from the last 25 seconds
-----------------
BACKGROUND THREAD
-----------------
srv_master_thread loops: 10665 srv_active, 0 srv_shutdown, 244985 srv_idle
srv_master_thread log flush and writes: 255566
----------
SEMAPHORES
----------
OS WAIT ARRAY INFO: reservation count 1747533
OS WAIT ARRAY INFO: signal count 1792563
Mutex spin waits 1149803, rounds 6105609, OS waits 107205
RW-shared spins 5797082, rounds 112260476, OS waits 1570178
RW-excl spins 64167, rounds 1886808, OS waits 42317
Spin rounds per wait: 5.31 mutex, 19.36 RW-shared, 29.40 RW-excl
------------------------
LATEST FOREIGN KEY ERROR
------------------------
2014-08-08 09:26:11 1754 Error in foreign key constraint of table trillium_data/#sql-510_460:
 FOREIGN KEY (company_id) REFERENCES company(conpany_id):
Cannot resolve column name close to:
)
------------
TRANSACTIONS
------------
Trx id counter 256766
Purge done for trx's n:o < 256306 undo n:o < 0 state: running but idle
History list length 1559
LIST OF TRANSACTIONS FOR EACH SESSION:
---TRANSACTION 256325, not started
MySQL thread id 1120, OS thread handle 0x1754, query id 56155 localhost 127.0.0.1 root init
SHOW ENGINE innodb STATUS
---TRANSACTION 0, not started
MySQL thread id 1119, OS thread handle 0x87c, query id 56126 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
28498265 OS file reads, 1898344 OS file writes, 194654 OS fsyncs
0.84 reads/s, 16384 avg bytes/read, 0.92 writes/s, 0.40 fsyncs/s
-------------------------------------
INSERT BUFFER AND ADAPTIVE HASH INDEX
-------------------------------------
Ibuf: size 1, free list len 296, seg size 298, 80598 merges
merged operations:
 insert 2226499, delete mark 1017126, delete 33664
discarded operations:
 insert 46998, delete mark 0, delete 0
Hash table size 69257, node heap has 1 buffer(s)
0.56 hash searches/s, 2.64 non-hash searches/s
---
LOG
---
Log sequence number 68198765705
Log flushed up to   68198765705
Pages flushed up to 68198765705
Last checkpoint at  68198765705
0 pending log writes, 0 pending chkp writes
80801 log i/o's done, 0.12 log i/o's/second
----------------------
BUFFER POOL AND MEMORY
----------------------
Total memory allocated 17006592; in additional pool allocated 0
Dictionary memory allocated 574008
Buffer pool size   1024
Free buffers       767
Database pages     256
Old database pages 0
Modified db pages  0
Pending reads 0
Pending writes: LRU 0, flush list 0, single page 0
Pages made young 35874501, not young 3712939362
0.00 youngs/s, 0.00 non-youngs/s
Pages read 28455284, created 855601, written 1702138
0.84 reads/s, 0.28 creates/s, 0.56 writes/s
Buffer pool hit rate 946 / 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: 256, unzip_LRU len: 0
I/O sum[35]: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 3420, state: sleeping
Number of rows inserted 52721481, updated 22524132, deleted 173611, read 1642731205
0.00 inserts/s, 0.00 updates/s, 0.00 deletes/s, 0.00 reads/s
----------------------------
END OF INNODB MONITOR OUTPUT
============================

最佳答案

您应该在 REFERENCES company(...) 中使用 company_id 而不是 conpany_id

ALTER TABLE person ADD CONSTRAINT person_company_FK 
FOREIGN KEY (company_id) REFERENCES company(company_id) ;

只是打字错误:)

关于mysql - 不允许添加外键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25204661/

有关mysql - 不允许添加外键的更多相关文章

  1. ruby - 我需要将 Bundler 本身添加到 Gemfile 中吗? - 2

    当我使用Bundler时,是否需要在我的Gemfile中将其列为依赖项?毕竟,我的代码中有些地方需要它。例如,当我进行Bundler设置时:require"bundler/setup" 最佳答案 没有。您可以尝试,但首先您必须用鞋带将自己抬离地面。 关于ruby-我需要将Bundler本身添加到Gemfile中吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/4758609/

  2. ruby - 将 Bootstrap Less 添加到 Sinatra - 2

    我有一个ModularSinatra应用程序,我正在尝试将Bootstrap添加到应用程序中。get'/bootstrap/application.css'doless:"bootstrap/bootstrap"end我在views/bootstrap中有所有less文件,包括bootstrap.less。我收到这个错误:Less::ParseErrorat/bootstrap/application.css'reset.less'wasn'tfound.Bootstrap.less的第一行是://CSSReset@import"reset.less";我尝试了所有不同的路径格式,但它

  3. ruby - 续集在添加关联时访问many_to_many连接表 - 2

    我正在使用Sequel构建一个愿望list系统。我有一个wishlists和itemstable和一个items_wishlists连接表(该名称是续集选择的名称)。items_wishlists表还有一个用于facebookid的额外列(因此我可以存储opengraph操作),这是一个NOTNULL列。我还有Wishlist和Item具有续集many_to_many关联的模型已建立。Wishlist类也有:selectmany_to_many关联的选项设置为select:[:items.*,:items_wishlists__facebook_action_id].有没有一种方法可以

  4. ruby-on-rails - RSpec:避免使用允许接收的任何实例 - 2

    我正在处理旧代码的一部分。beforedoallow_any_instance_of(SportRateManager).toreceive(:create).and_return(true)endRubocop错误如下:Avoidstubbingusing'allow_any_instance_of'我读到了RuboCop::RSpec:AnyInstance我试着像下面那样改变它。由此beforedoallow_any_instance_of(SportRateManager).toreceive(:create).and_return(true)end对此:let(:sport_

  5. ruby - 可以通过多少种方法将方法添加到 ruby​​ 对象? - 2

    当谈到运行时自省(introspection)和动态代码生成时,我认为ruby​​没有任何竞争对手,可能除了一些lisp方言。前几天,我正在做一些代码练习来探索ruby​​的动态功能,我开始想知道如何向现有对象添加方法。以下是我能想到的3种方法:obj=Object.new#addamethoddirectlydefobj.new_method...end#addamethodindirectlywiththesingletonclassclass这只是冰山一角,因为我还没有探索instance_eval、module_eval和define_method的各种组合。是否有在线/离线资

  6. ruby - 如何在 Ruby 中向现有方法定义添加语句 - 2

    我注意到类定义,如果我打开classMyClass,并在不覆盖的情况下添加一些东西我仍然得到了之前定义的原始方法。添加的新语句扩充了现有语句。但是对于方法定义,我仍然想要与类定义相同的行为,但是当我打开defmy_method时似乎,def中的现有语句和end被覆盖了,我需要重写一遍。那么有什么方法可以使方法定义的行为与定义相同,类似于super,但不一定是子类? 最佳答案 我想您正在寻找alias_method:classAalias_method:old_func,:funcdeffuncold_func#similartoca

  7. ruby-on-rails - 添加回形针新样式不影响旧上传的图像 - 2

    我有带有Logo图像的公司模型has_attached_file:logo我用他们的Logo创建了许多公司。现在,我需要添加新样式has_attached_file:logo,:styles=>{:small=>"30x15>",:medium=>"155x85>"}我是否应该重新上传所有旧数据以重新生成新样式?我不这么认为……或者有什么rake任务可以重新生成样式吗? 最佳答案 参见Thumbnail-Generation.如果rake任务不适合你,你应该能够在控制台中使用一个片段来调用重新处理!关于相关公司

  8. ruby - 我如何添加二进制数据来遏制 POST - 2

    我正在尝试使用Curbgem执行以下POST以解析云curl-XPOST\-H"X-Parse-Application-Id:PARSE_APP_ID"\-H"X-Parse-REST-API-Key:PARSE_API_KEY"\-H"Content-Type:image/jpeg"\--data-binary'@myPicture.jpg'\https://api.parse.com/1/files/pic.jpg用这个:curl=Curl::Easy.new("https://api.parse.com/1/files/lion.jpg")curl.multipart_form_

  9. 使用canal同步MySQL数据到ES - 2

    文章目录一、概述简介原理模块二、配置Mysql使用版本环境要求1.操作系统2.mysql要求三、配置canal-server离线下载在线下载上传解压修改配置单机配置集群配置分库分表配置1.修改全局配置2.实例配置垂直分库水平分库3.修改group-instance.xml4.启动监听四、配置canal-adapter1修改启动配置2配置映射文件3启动ES数据同步查询所有订阅同步数据同步开关启动4.验证五、配置canal-admin一、概述简介canal是Alibaba旗下的一款开源项目,Java开发。基于数据库增量日志解析,提供增量数据订阅&消费。Git地址:https://github.co

  10. ruby-on-rails - 在 Ruby on Rails 中添加 boolean 列值 - 2

    我正在开发一个创建网络博客的RubyonRails项目。我希望将一个名为featured的boolean数据库字段添加到Post模型中。该字段应该可以通过我添加的事件管理界面进行编辑。我使用了以下代码,但我什至没有在网站上显示另一列。$railsgeneratemigrationaddFeaturedfeatured:boolean$rakedb:migrate我是RubyonRails的新手,非常感谢任何帮助。我的index.html.erb文件中的相关代码(views):FeaturedPost架构.rb:ActiveRecord::Schema.define(:version=>

随机推荐