My question is how to edit original table in laravel migration?
That is, suppose to there are two table have as Teacher and Children. As well as Student table has foreign key as TeacherId. (Teacher_id refer to TeacherId in Teacher table)
Some error reason I want to drop Teacher table or edit Teacher table. Then general error occur without original key table cannot had foreign key.
So how can I drop the Teacher table or edit Teacher table without drop Student table?
Have some way to do that?
First you need to drop the foreign key on the your Student table (or maybe the teacher_id column entirely if you no longer have a Teacher table), then you should be able to drop the Teacher table assuming no more foreign keys are referencing the Student table.
You should code your laravel migrations in a way, so you can revert it all back, under down() method.
Sometimes you get stuck, in a way that, migrate or migrate:rollback command produces an error.
Then you should modify your db structure, by hand (using mysql command, workbench, or any other sql tools), to bring your migrations to working again...
And finally targetting your question...
You cannot create table and add foreign constraints in one commit to db, so in order, to do that, you need to add new declaration in your migration up() like Table:... after Schema:create... and add your foreign keys here. Remember to do the opposite in down() method...
Related
I am trying to add attendances to the attendance table and if attendance of the give date and student id is already in the table I need to update the attendance type.
But the upsert is only inserting and it does not ever update.
SmStudentAttendance::upsert($studentRows, ['student_id', 'attendance_date'], ['attendance_type']);
It's too late but it worked for me. Try this in a migration:
$table->unique(['student_id', 'attendance_date']);
Or you can add unique constraints to the columns in phpmyadmin (or a similar tool)
upsert works exactly like
on duplicate key update on MySQL
which needs a unique constraint violation to update instead of an insert.
So check if you have such a violation, directly in the database.
Also you can read more about with an example for example at https://www.amitmerchant.com/insert-or-update-multiple-records-using-upsert-in-laravel8/
I am trying to alter table product to add a constraint of type foreign key for field petCat_ID so that it references table petCategory(ID). I just created table petCat_ID and i am getting a "Cannot add or update a child row" error.
This is the commands I performed to get this error:
Alter table product
-> ADD CONSTRAINT FK_petCatID
-> FOREIGN KEY (petCat_ID)
-> REFERENCES productCategory(ID);
Any help or tips would be greatly appreciated! Note: petCat_ID is in table product and productCategory is a different table.
In my comments I've mentioned that I need a clearer idea of what kind of database structure you have, but I have a series of things that will help you work through the problem you're having.
If an ALTER statement isn't working, and you have good syntax, it is because what you are doing conflicts with an already present rule.
Sometimes, doing a DROP TABLE command, followed by creating the table again can fix problems. This can be problematic if there are dependencies that keep you from dropping the table.
When things get dire, try looking at the script you used to make the DB in the first place. Modify it and see if you can get the properties you want. Once you do, make a new database table structure and migrate your table entries over to the new database from the old one.
I made a github repository here wherein I made a third normal form version of what the customer facing Amtrack database would look like, and even wrote scripts to add data to the tables, with examples. There are images showing the ER structure. I included my creation script, broken into each table's creation in specific order. It should be a good reference for how to assign table relationships, and that will give you a good idea of what you can alter. Disclaimer I wrote it for SSMS, but I don't believe I used anything SSMS specific I THINK that code should work in MySQL.
I'm new to programming so please forgive my ignorance. I'm trying to get MySQL tables to update each other. For example: When I insert data into a Primary Key column in one table, it does not appear in it's Foreign Key column in another table.
Does anyone have any advice on this issue?
Thank you!
Additional Information: I used phpMyAdmin to create my tables and then added SQL code to create the Foreign Keys. Example of code is below.
ALTER TABLE CourseSchedule
ADD FOREIGN KEY (CourseId)
REFERENCES Course(CourseId)
I believe what we have here is a slight misunderstanding of the documentation.
For storage engines supporting foreign keys, MySQL rejects any INSERT
or UPDATE operation that attempts to create a foreign key value in a
child table if there is no a matching candidate key value in the
parent table.
When an UPDATE or DELETE operation affects a key value in the parent
table that has matching rows in the child table, the result depends on
the referential action specified using ON UPDATE and ON DELETE
subclauses of the FOREIGN KEY clause. MySQL supports five options
regarding the action to be taken, listed here
What your foreign key actually does is to make sure that you cannot insert values into your CourseSchedule table that do not correspond to an entry in the Courses table.
To give you an example, suppose you were to try to enter schedule a python course for every wednesday at 9:00 but you dont' actually have an entry for python in your Courses table. Then mysql will refuse to create that entry. Mysql cannot do the reverse. It doesn't know details about your python course. So it cannot automatically create a entry in the Courses table for you. Similarly, if oyu created an entr in the courses table. Mysql cannot automatically create a CourseSchedule for you because it doesn't know at what time it should be scheduled.
I've created a simple database relation consisting of the entities entry and keywords.
Any entry can have n keywords, but each keyword may only exist once in the keywords table.
To realize this, I've created three tables: entries, keywords and entries2keywords. I'd like to maintain semantic integrity, so a DELETE or UPDATE procedure should propagate to the related tables.
Another requirement for the database setup is to be re-entrant, meaning that I can just re-run the creation script in which case all data should simply be deleted, as in DROP TABLE.
However, my current script fails with a #1217 error when re-running it:
#1217 - Cannot delete or update a parent row: a foreign key constraint fails
I've linked to the database script here because it's too large to paste.
So, than, dropping entries2keywords (table with constraints) will solve the issue :)
I'm try to create a foreign key between tables by using MySQL Workbench. But I don't know why I can't tick the checkbox to select a field in order to map with another field in another table. Maybe it require both field has the same type (and other conditions??)
So can you tell me the criteria to create relationship using foreign key and how to do it in MySQL Workbench?
I had this problem too. The reason I couldn't create the relationship was as you say the types weren't exactly the same. I had an unsigned int as my primary key and a signed int as my foreign key, so the software wouldn't allow me to create the relationship. Would have been nice if the software came up with an alert or some kind of user feedback highlighting it's objection to checking that box.
I'm not a user of MySQL Workbench, but make sure you're using a storage engine that supports foreign keys in the first place. (for example, InnoDB)
See the MySQL documentation for the requirements necessary for a foreign key relationship.
I had the same issue. Found a workaround:
After you have entered name of foreignkey constraint and selected the referenced table click "next" without selecting the column names.
In this step you will see the create sql script of new constraint.
Edit it manually: enter the referenced column name and the column name of fk.
Then click finish. The script will be executed.
For recheck try to open table alter window again and you will see that the column checkbox is ticked now in foreign key tab.
When you edit a table in the EER diagram editor, there's a "Foreign Keys" tab. You can set the foreign keys between tables there. (Workbench 5.2.36)
I am facing the same problem with MySql Workbench. I have one char(5) (in table 1) as my primary key and another char(5) (in table 2) as a foreign key. But MySql Workbench won't let me create the relationship. I am using INNODB.