Django Mysql Migration Error 150 with FOREIGN KEY - mysql

I need help debugging a migration.
I tried to execute each query step by step to found where django migration stops.
This query fails:
mysql> ALTER TABLE `elezioniAgendaCore_comments` ADD CONSTRAINT `elezioniAgendaCore_comments_uid_id_670175fa07fa7b47_fk_user_uid` FOREIGN KEY (`uid_id`) REFERENCES `user` (`uid`);
With Error:
ERROR 1005 (HY000): Can't create table 'activedoc.#sql-3f7_2b' (errno: 150)
I have tow tables.
TABLE user:
mysql> show columns from user;
+-------------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------------+--------------+------+-----+---------+----------------+
| uid | bigint(20) | NO | PRI | NULL | auto_increment |
| name | varchar(50) | YES | MUL | NULL | |
| surname | varchar(50) | YES | MUL | NULL | |
| email | varchar(100) | YES | MUL | NULL | |
+-------------------+--------------+------+-----+---------+----------------+
TABLE elezioniAgendaComments:
mysql> show columns from elezioniAgendaCore_comments;
+------------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| signature | varchar(255) | NO | | NULL | |
| message | longtext | NO | | NULL | |
| created | datetime | NO | | NULL | |
| published | tinyint(1) | NO | | NULL | |
| adempimento_id | int(11) | NO | MUL | NULL | |
| parentComment_id | int(11) | YES | MUL | NULL | |
| uid_id | bigint(20) | NO | | NULL | |
+------------------+--------------+------+-----+---------+----------------+

I found one table was created with MyISAM engine:
SHOW CREATE TABLE user;
[...]
) ENGINE=MyISAM AUTO_INCREMENT=307 DEFAULT CHARSET=latin1 |
The other table:
mysql> SHOW CREATE TABLE elezioniAgendaCore_comments;
[...]
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
Then I migrated to InnoDB both tables. No errors this time.

Related

Can't add foreign key with alter table query

I'm trying to add a foreign key to my table (oc_booking) but when I do the alter table query it returns "ERROR 1215 (HY000): Cannot add foreign key constraint" and I don't know why.
I tried using SQL (terminal) and HeidiSQL (a tool similar to MySQL Workbench).
This is the reference table (oc_user):
+---------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------+--------------+------+-----+---------+----------------+
| user_id | int(11) | NO | PRI | NULL | auto_increment |
| user_group_id | int(11) | NO | | NULL | |
| username | varchar(20) | NO | | NULL | |
| password | varchar(40) | NO | | NULL | |
| salt | varchar(9) | NO | | NULL | |
| firstname | varchar(32) | NO | | NULL | |
| lastname | varchar(32) | NO | | NULL | |
| email | varchar(96) | NO | | NULL | |
| image | varchar(255) | NO | | NULL | |
| code | varchar(40) | NO | | NULL | |
| ip | varchar(40) | NO | | NULL | |
| status | tinyint(1) | NO | | NULL | |
| date_added | datetime | NO | | NULL | |
+---------------+--------------+------+-----+---------+----------------+
This is the table where I want to add a foreign key constraint (oc_booking):
+----------------+-----------+------+-----+-------------------+-----------------------------+
| Field | Type | Null | Key | Default | Extra |
+----------------+-----------+------+-----+-------------------+-----------------------------+
| appointment_id | int(11) | NO | PRI | NULL | auto_increment |
| id_supplier | int(11) | NO | MUL | NULL | |
| id_user | int(11) | NO | | NULL | |
| date | timestamp | NO | | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
+----------------+-----------+------+-----+-------------------+-----------------------------+
And this is my query:
ALTER TABLE oc_booking ADD FOREIGN KEY (id_user) REFERENCES oc_user(`user_id`);

mysql won´t create me an foreign key, cause no index in referenced table (but index exist!)

Moin moin (like we in Hamburg say),
i wanted to add a column with a foreign key in a existing table with filled records. and referencing the key to another table with primary key. so far so good.
But i can´t cause mysql says me that the index for the referenced table is not existing. But it actually does. so i don´t know, what´s the point is.
the error:
2017-04-11 19:21:08 0x1770 Error in foreign key constraint of table animals/#sql
-7a0_7:
foreign key(pet_id) references pet (pet_id):
Cannot find an index in the referenced table where the
referenced columns appear as the first columns, or column types
in the table and the referenced table do not match for constraint.
the referenced table:
+---------+-----------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------+-----------------+------+-----+---------+----------------+
| pet_id | int(3) unsigned | NO | PRI | NULL | auto_increment |
| name | varchar(20) | YES | | NULL | |
| owner | varchar(20) | YES | | NULL | |
| species | varchar(10) | YES | | NULL | |
| sex | char(1) | YES | | NULL | |
| birth | date | YES | | NULL | |
| death | date | YES | | NULL | |
+---------+-----------------+------+-----+---------+----------------+
7 rows in set (0.00 sec)
and the index:
+-------+------------+----------+--------------+-------------+-----------+------
-------+----------+--------+------+------------+---------+---------------+
| Table | Non_unique | Key_name | Seq_in_index | Column_name | Collation | Cardi
nality | Sub_part | Packed | Null | Index_type | Comment | Index_comment |
+-------+------------+----------+--------------+-------------+-----------+------
-------+----------+--------+------+------------+---------+---------------+
| pet | 0 | PRIMARY | 1 | pet_id | A |
9 | NULL | NULL | | BTREE | | |
| pet | 1 | pet_id | 1 | pet_id | A |
9 | NULL | NULL | | BTREE | | |
| pet | 1 | pet_id | 2 | name | A |
9 | NULL | NULL | YES | BTREE | | |
| pet | 1 | pet_id | 3 | owner | A |
9 | NULL | NULL | YES | BTREE | | |
| pet | 1 | pet_id | 4 | species | A |
9 | NULL | NULL | YES | BTREE | | |
| pet | 1 | pet_id | 5 | sex | A |
9 | NULL | NULL | YES | BTREE | | |
| pet | 1 | pet_id | 6 | birth | A |
9 | NULL | NULL | YES | BTREE | | |
| pet | 1 | pet_id | 7 | death | A |
9 | NULL | NULL | YES | BTREE | | |
+-------+------------+----------+--------------+-------------+-----------+------
-------+----------+--------+------+------------+---------+---------------+
8 rows in set (0.10 sec)
if it helps............
UPDATE: result of SHOW CREATE TABLE pet:
| pet | CREATE TABLE `pet` (
`pet_id` int(3) unsigned NOT NULL AUTO_INCREMENT,
`name` varchar(20) DEFAULT NULL,
`owner` varchar(20) DEFAULT NULL,
`species` varchar(10) DEFAULT NULL,
`sex` char(1) DEFAULT NULL,
`birth` date DEFAULT NULL,
`death` date DEFAULT NULL,
PRIMARY KEY (`pet_id`),
KEY `pet_id` (`pet_id`,`name`,`owner`,`species`,`sex`,`birth`,`death`)
) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8 |

"Foreign key constraint is incorrectly formed" despite same data type

I wanted to add a foreign key Enrols.mid that references Member.id, but after a few tries (adding index, creating keys, googleing) it still isn't working.
The two columns have the exact same data type, why adding a foreign key isn't working?
MariaDB [alumni]> alter table Enrols add foreign key (`mid`) references Member.id;
ERROR 1005 (HY000): Can't create table `alumni`.`#sql-31e_21d` (errno: 150 "Foreign key constraint is incorrectly formed")
MariaDB [alumni]> desc Member;
+-----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+-------+
| id | char(8) | NO | PRI | | |
| cName | varchar(8) | NO | | NULL | |
| eName | varchar(40) | NO | | NULL | |
| gender | char(1) | NO | | NULL | |
| org | varchar(40) | YES | | NULL | |
| pos | varchar(20) | YES | | NULL | |
| hobby | int(11) | NO | | NULL | |
| issueDate | date | NO | | NULL | |
| note | text | YES | | NULL | |
+-----------+-------------+------+-----+---------+-------+
9 rows in set (0.01 sec)
MariaDB [alumni]> desc Enrols;
+-------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| id | int(11) | YES | | NULL | |
| aid | int(11) | NO | PRI | NULL | |
| mid | char(8) | NO | PRI | | |
| paid | date | YES | | NULL | |
+-------+---------+------+-----+---------+-------+
4 rows in set (0.01 sec)
MariaDB [alumni]>
Like #Damien_The_Unbeliever said in their comment, it should be references Member(id) .
Your . syntax means: reference table id in Schema Member (But you want to reference column id in table Member.

Mysql table relationship and can't add foreign key it show can't create

I have 2 tables.
manifast
+-------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+------------------+------+-----+---------+----------------+
| manifast_id | int(11) unsigned | NO | PRI | NULL | auto_increment |
| description | text | NO | | NULL | |
| title | text | NO | | NULL | |
+-------------+------------------+------+-----+---------+----------------+
day_sequence;
+-----------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+------------------+------+-----+---------+----------------+
| day_sequence_id | int(11) unsigned | NO | PRI | NULL | auto_increment |
| day_number | int(11) | NO | | NULL | |
| day_start | int(11) | NO | | NULL | |
| manifast_id | int(11) | NO | | NULL | |
+-----------------+------------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)
I wanna connect those two columns and use this command.
ALTER TABLE day_sequence
ADD CONSTRAINT fk_manifast
FOREIGN KEY (manifast_Id)
REFERENCES manifast(manifast_Id);
and it show this error.How can i solve?
The specified relation was unable to be created.
MySQL said: Can't create table 'projectx.#sql-3e0_4' (errno: 150)
Manifast_id also needs to be unsigned int, looks like it is not.
Try fixing the other manifast id to unsigned then try doing it like this
ALTER TABLE day_sequence
ADD CONSTRAINT fk_manifast
FOREIGN KEY (manifast_Id)
REFERENCES (manifast.manifast_Id);

How to create primary key using multiple columns & Auto increment id?

This is my tables structures
mysql> DESCRIBE sections;
+----------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+----------------+
| sec_id | int(5) | NO | PRI | NULL | auto_increment |
| sec_name | varchar(45) | YES | | NULL | |
| sec_type | tinyint(4) | YES | | NULL | |
+----------+-------------+------+-----+---------+----------------+
mysql> DESCRIBE subjects;
+-----------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+-------------+------+-----+---------+----------------+
| sub_id | int(5) | NO | PRI | NULL | auto_increment |
| sub_name | varchar(45) | YES | | NULL | |
| sections_sec_id | int(5) | NO | MUL | NULL | |
+-----------------+-------------+------+-----+---------+----------------+
mysql> DESCRIBE chapters;
+-----------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+-------------+------+-----+---------+----------------+
| chp_id | int(5) | NO | PRI | NULL | auto_increment |
| chp_name | varchar(45) | YES | | NULL | |
| subjects_sub_id | int(5) | NO | MUL | NULL | |
| sections_sec_id | int(5) | NO | MUL | NULL | |
+-----------------+-------------+------+-----+---------+----------------+
mysql> DESCRIBE questions;
+-----------------+-----------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------+-----------+------+-----+---------+----------------+
| que_id | int(11) | NO | PRI | NULL | auto_increment |
| que_text | text | YES | | NULL | |
| que_created | timestamp | YES | | NULL | |
| chapters_chp_id | int(5) | NO | MUL | NULL | |
| subjects_sub_id | int(5) | NO | MUL | NULL | |
| sections_sec_id | int(5) | NO | MUL | NULL | |
+-----------------+-----------+------+-----+---------+----------------+
mysql> DESCRIBE answers;
+------------------+-----------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------------+-----------+------+-----+---------+----------------+
| ans_id | int(11) | NO | PRI | NULL | auto_increment |
| ans_text | text | YES | | NULL | |
| ans_created | timestamp | YES | | NULL | |
| questions_que_id | int(11) | NO | MUL | NULL | |
| chapters_chp_id | int(5) | NO | MUL | NULL | |
| subjects_sub_id | int(5) | NO | MUL | NULL | |
| sections_sec_id | int(5) | NO | MUL | NULL | |
+------------------+-----------+------+-----+---------+----------------+
Let's assume 'Answers' table
I created the 'ans_id' as the primary key of 'Answers' table, created a another UNIQUE key using (ans_id,questions_que_id,chapters_chp_id,subjects_sub_id,sections_sec_id)
What i want do.
create primary key for 'Answers' table using (ans_id,questions_que_id,chapters_chp_id,subjects_sub_id,sections_sec_id) columns and create Trigger before INSERT to create 'auto_increment' id.
Finally i want to accomplish this
Definition: Primary key is minimal set of attributes needed to determine every row in a table.
UNIQUE index should be {questions_que_id} + {chapters_chp_id} + {subjects_sub_id} +{sections_sec_id} and make sure it is combined as one index with these 4 columns, if you set it as different 4 indexes than you will probably achieve undesired outcome, as Zohar already pointed. From my experience, it could be unpractical to use composite keys like this. For basic table operations (CRUD) you will have to provide a lot of parameters.
Maybe to try this way: autoincremental numeric id as a primary key, the rest of important attributes as UNIQUE index. Then you could count already inserted answers to make sure right number will be assigned to ans_id. Hint: it's maybe easier to achive this from your application layer, not as a trigger or procedure for MySQL. If your users will provide answers as step-by-step, then just save a timestamp and you will know which answer is older so you don't need ans_id at all.