I'm using Alembic with Flask-SQLAlchemy, and I'm trying to remove a unique constraint on a column in one of my tables. Looking over the migrations - this constraint was never named: sa.UniqueConstraint('title'), obviously when I update the model, Alembic can't pick up that I removed unique=True off of it.
How do I create a migration that removes the constraint?
I tried this, but it didn't work:
with op.batch_alter_table('note', schema=None) as batch_op:
batch_op.create_unique_constraint('title_uniq', 'title')
batch_op.drop_constraint('title_uniq', type_='unique')
What ended up working was I had to have two migrations - one to create a named index and one to remove it.
Related
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.
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...
I need to create the database schema and include it in my software requirements specification for my school project, however, when I try to create a relationship between 2 tables, I get Error: Missing index on column(s).
I think #HazarathChillara has this right; you need to create primary, unique, or index keys.
You said every table has an primary key, but did you make each foreign and referenced key an index as well? It sounds like you neglected to properly set up your table structure; I only get the error when I don't have a primary key or index on the particular columns I'm working with.
"MySQL requires indexes on foreign keys and referenced keys so that foreign key checks can be fast and not require a table scan"
You can just put an INDEX on the foreign key (often my referenced key is a primary key anyway, so I don't need any additional key on that column).
This error appears only when you neglect table structure. Make sure that you Indexed a foreign key as well. you can see i marked how could i select my foreign key as index.In this Image I am indexing selected, 'sr' my foreign key
As Usman Khan said you have to go to the structure tab of the particular table and clicked on more options and select 'INDEX' for the foreign key.
the below image will help you how to do it
I think i have another simple solve,
thing is, phpMyAdmin wont allow the addition of foreign keys to an already available data entry, so here is the my simple solve,
1. ensure to backup your database
2. confirm that your data was backed-up securely, recommended Offline backups
4. delete all data entries in all tables that will be part of the new relationship.
5. now Create the relevant relationships.
6. be sure you have created all required and preferred relations to avoid the need to
export data again
I am trying to drop a table in MySQL but its complaining about "Cannot delete or update a parent row: a foreign key constraint fails"...
Is there a way to find out all the table dependencies and objects dependent on it?
For sybase, DBArtisan gives a really easy way to find dependecies. Is there any such tool for MySQL?
SHOW CREATE TABLE mytable;
It'll show you (along with some other stuff) the foreign key relationships for the table.
The relation of PRIMARY is equal to fk_student_single_user1. So i must remove one of them.
The problem is, i can't remove or rename PRIMARY in workbench, the program does not allow, and if i delete fk_student_single_user1, i also delete the foreign key. The only way is delete PRIMARY in the phpmyadmin.
But i think that exists any problem in my eer model, it is supposed an export without bugs.
I deleted my previous column id, because two foreign keys can be the primary key of the table.
How i can solve that?
Try deleting the foreign key, dropping the needless fkey index, and re-adding the foreign key using plain sql (alter table...) rather than your GUI.
The SQL spec requires a unique index on the target column, so there's no reason to add an extra (non-unique) index on top.
If MySQL still adds the index, you might want to report it as a bug (as well as to http://sql-info.de/mysql/gotchas.html).
If not, you might want to investigate whether the index was added by your GUI in the first place. And if so, report the issue as a bug to the GUI's creator.