I have the following problem in my current Symfony 6 project. I have a Many to Many Relationship between two tables in my Database. Therefore a third table for the references got created.
Afterwards, I created an Entity for this thie reference table which I needed for a query. However, if I now want to create a new migration with the command php bin/console make:migration.
I always get the error "table with name "database.table" already exists"
I think the problem occurs because, obviously the table for this entity already exists and the migration command wants to create it. But how do I solve this issue?
Thanks for any ideas in advance. :)
Maybe you are trying to build ManyToMany with extra fields?
Then this is not a simple ManyToMany anymore: it's now a OneToMany + the inverse side of the ManyToOne relationship.
More details is here https://symfonycasts.com/screencast/collections/many-to-many-extra-fields
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.
I use code first convention and mysql database in asp.net application. First I created a data model. Second I created database using add-migration and update database. I see in MySQLWorkbench that my database was created.
After that I run my app to see how database work. Before one of views is created I 'ask' database for a list of users. The problem is that during linq query to get list of users there is thrown an exception. Message is Table 'Events' already exists. I do not understand this error. Yes, all tables in the database exits. There is not direct relation between these to tables.
I have stared looking for the answer in the Internet. I found only one solution. I droped database, deleted all migrations, created new migration and update-database, but the this solutions does not solve the problem.
Any ideas?
This problem have number 1050 in MySQL.
In my Seed method I can insert objects into database, and it works. But during app is running the exception is thrown. I try:
DROP TABLE IF EXISTS Events;
REPAIR TABLE Events;
I execute this queries in MySQLWorkbench and restart the application. Then I have "new" exception that: "Table 'Documents' already exists". When I try
DROP TABLE IF EXISTS Documents;
REPAIR TABLE Documents;
MySQLWorkbench: Error Code: 1217. Cannot delete or update a parent row: a foreign key constraint fails. So the problem still exists. Now I have exception "Table 'Documents' already exists". I check my Up method in migration class the first table which is created is Events, the second Documents. So I think that I should repair all tables, becouse in database-update command must create wrong database. But why it earlier works(?) I still do not know.
im new to entity framework , i created my entities from reverse engineering from a Mysql database that i have created in mysqlworkbench , and then i added some foreign keys in my entities and then i added a migration and tried to update my database but an error occured and it indicates that: "Table 'pidev.pidev.personal' doesn't exist".
"pidev" is the name of my database. personal is a super class for two other subclasses "candiadte" and "employee" and im using TPH as inheritance strategy.
PLZ i need some help.
thank you all for your replies , I SOLVED the problem in the UP() and DOWN()method in the migration file i removed the name of the database in each line like this "DropPrimaryKey("pidev.personal");" ==> DropPrimaryKey("personal");
I have already existing DataBase inside SQL server 2008 around (300 tables) so I faced the following problems when i try to create an entity framework .edmx file:
If I specify to map all the 300 tables at once, the visual Studio will hang.
So I decide to include only the tables which I currently need, then the mapping will work fine. But if after that I add a new table to them, a Foreign key error will occur. So I have to delete the existing models and add them again with the new table. So the FK error will be removed.
So can anyone advice on how to overcomes these problems?
The error I usually got when adding a new table to the .edmx file will look something similar to.
Problem in mapping fragments starting at lines 2186, 2265:Foreign key constraint 'SDOrgPostalAddr_FK2' from table SDOrgPostalAddr (POSTALADDR_ID) to table AaaPostalAddress (POSTALADDR_ID):: Insufficient mapping: Foreign key must be mapped to some AssociationSet or EntitySets participating in a foreign key association on the conceptual side.
while if I added the table from the beginning then no error will be displayed.
Regards
Here is a situation i could not resolve by myself.
I have an existing symfony 1.4 project with database, model and etc. I want to make a migration so I've done:
./symfony cc
./symfony doctrine:generate-migrations-db
./symfony doctrine:generate-migrations-model
All classes for the migration were created, so I've tried to apply the migration by:
./symfony doctrine:drop-db
./symfony doctrine:build-db
./symfony doctrine migrate
And the migration proccess crashes. It thros an error:
- SQLSTATE[1005]: General error: 1005 Can't create table 'database.#sql-6df_301' (errno: 150). Failing Query: "ALTER TABLE product ADD CONSTRAINT "product_product_group_id_product_group_id FOREIGN KEY (product_group_id) REFERENCES product_group(id) ON DELETE CASCADE
This is strange. I have more tables with relations and they are created, but that one fails. I've check everything i can think of. The indexes types, the table types, the phase of the moon - everything seems to be OK. I've try to SET FOREIGN_KEY_CHEKS=0 to chase some ghosts, but NADA!
The error still occures.
Does anybody knows what is happening or a kind of solution?
Any suggestion?
You should not use :generate-migrations-models . Try it with the sequence
./symfony cc
./symfony doctrine:generate-migrations-db
./symfony doctrine:drop-db
./symfony doctrine:build-db
./symfony doctrine migrate
The line
./symfony doctrine:generate-migrations-models
uses the model classes to generate the migrations. It's not always a good idea, because you could have forgotten models messing things up. In order to clean those forgotten models use the following before you start those commands above.
./symfony doctrine:clean-model-files
I've cracked it up.
In a some hilarious reason the auto-generated migration classes are named like this:
1311678541_add<table-name>.php
1311678542_add<table-name>.php
1311678543_add<table-name>.php
...
13116785578_addfks.php
...
1311678579_add<table-name>.php
1311678580_add<table-name>.php
1311678582_addproductgroup.php
The 13116785578_addfks.php file appears before 1311678582_addproductgroup.php.
The file 13116785578_addfks.php containing a class which creates all relations (foreign keys and constraints), but it tries to add constraint on a non existing table (in my case product_group, which will be created last). So, the SQL error means YOU ARE TRYING TO CREATE A RELATION WITH UNEXISTING TABLE, YOU MORON! :)
My soltion is to rename the 13116785578_addfks.php to 13116785590_addfks.php as it forces the doctrine merge machine to execute it as a last step. When executed all tables are already made and MySQL server is happy!
So, what about the reason for this miss-ordering?
Probably it caused by mixi'n up the to tasks doctrine:generate-migrations-db and doctrine:generate-migrations-models,
but the Symfony's Migration tutorial isn't very clear with this.
Why I use them both?
When I done ./symfony doctrine:generate-migration-db only the half of classes for the migration was created. It is strange - there are only general tables and no related ones! So I called doctrine:generate-migrations-models.
Conclusion
Another question raises: Why doctrine:generate-migrations-db does not generate migration classes for related tables of the model?