symfony doctrine build-sql error - schema

I have some big problems with symfony and doctrine at the beginning of a new project. I have created database diagram with mysql workbench, inserted the sql into phpmyadmin and then I've tried symfony doctrine:build-schema to generate the YAML schema.
It generates a wrong schema (relations don't have on delete/on update) and after this I've tried symfony doctrine:build --sql and symfony doctrine:insert-sql
The insert-sql statement generates error (can't create table ... failing query alter table add constraint ....), so I've decided to take a look over the generated sql and I've found out some differences between the sql generated from mysql workbench (which works perfect, including relations) and the sql generated by doctrine.
I'll be short from now: I have to tables, EVENT and FORM and a 1 to n relation (each event may have multiple forms) so the correct constraint (generated with workbench) is
ALTER TABLE `form` ADD CONSTRAINT `fk_form_event1` FOREIGN KEY (`event_id`) REFERENCES `event` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;
doctrine generated statement is:
ALTER TABLE event ADD CONSTRAINT event_id_form_event_id FOREIGN KEY (id) REFERENCES form(event_id);
It's totally reversed and I am sure here is the error. What should I do? It's also correct like this?

It it broken, I've wrote the schema manually and it works perfect. I didn't want to do that because it was a very large file, but I've also learned from it!
Thank you guys!

To get both visual representation and automatic code generation you can use ORM Designer so you will have everything covered by one tool.

Related

Joomla : libraries mysql schema does not working for Insert records, add primary key, drop table command

On extension update, instead of using update SQL schema I have given a custom button like fix database under that extension. It working fine. Custom script executes from same schema folder structure i.e. "/sql/updates/mysql"
But some DDL commands are not working like INSERT, ALTER for adding primary key for already existing table, DROP to delete table.
I have checked the MysqlChangeItem.php file (using Joomla 3.8.10) under "libraries/src/Schema/ChangeItem" & found the different DDL commands which are handled but does not found about INSERT/adding primary key for existing table/drop table.
Can you please suggest the solution

Wrong DDL statement order: dropping index before dropping related foreign key constraint

I'm trying to update a target database with SQL Server Data Tools, using the Publish option. I've got both pre-deployment and post-deployment scripts with custom instructions.
Here's the problem: SSDT tries to drop an index numero from my target that doesn't exist in my reference schema, but fails because it is being used for foreign key enforcement by constraint fk_numero. This foreign key is being dropped later in the script since there's another change to be made on this table.
I have considered dropping fk_numero in my pre-deploy script, but it would fail anyway because of the DROP CONSTRAINT fk_numero that is called later in the generated script: since SSDT doesn't write IF EXISTS tests before dropping a constraint, it fails when trying to delete something that doesn't exist.
I have also tried to disable all foreign keys in my pre-deploy script with a NOCHECK CONSTRAINT ALL, hoping I'd then be able to drop my index, to no avail.
Is there an option in SSDT to specify whether you want it to generate DROP CONSTRAINT scripts? Or an option for instructions order? Or a way to hint to SSDT that it should test whether the constraint exists before trying to drop it?
You could try the "DROP objects in target but not in Project" option.
Are you using custom-made scripts to modify the schema?
I just encountered this issue when working with SSDT. Turns out it's a known bug, which was reported on Microsoft Connect. However, it doesn't seem to have been resolved.
UPDATE:
The May 2015 Update for SSDT includes a fix for this issue.

RedBean: How to delete all rows from all tables

I am using RedBean ORM. In order to create the schema I used the standard redbean approach of inserting data so that Redbean would auto-fit the schema to suit my needs. I put this in a script which would basically be used to build the schema when i need to initialize my database.
The problem is that RedBean keeps a row or 2 in each table (the ones that I initially inserted to get redbean to build the schema).
If it were a normal database, to erase all rows i would just drop the schema and rebuild it, but in this case that's not possible since the initial rows would still exist.
Unfortunately there isn't too much Redbean Q/A out there. Anyone know how I would do this using the Redbean interface?
I have tried
$listOfTables = R::$writer->getTables();
foreach($listOfTables as $table)
{
R::wipe($table);
}
Of course this doesn't work though. (It doesn't TRUNCATE the tables in the correct order so I get an error about another table using this key as a foreign link. It simply iterates in ABC order)
Fatal error: Uncaught [42000] - SQLSTATE[42000]: Syntax error or access violation: 1701 Cannot truncate a table referenced in a foreign key constraint (`redbeandb`.`research`, CONSTRAINT `research_ibfk_1` FOREIGN KEY (`ownEducationHistory_id`) REFERENCES `redbeandb`.`educationhistory` (`id`)) thrown in C:\Users\Rod\nginx-1.0.12\html\rb.php on line 105
If someone has a (redbean api) solution, it would be much appreciated. And hopefully this question can be beneficial to building up more RedBean Q/A here on Stackoverflow.
Use
R::nuke();
Yes, it will drop all tables but since RedBeanPHP creates all tables on the fly this is not a problem.
I know this is an old post but I figured I'd help out someone finding this today. You can tell mysql to ignore foreign key checks if you don't care about data integrity (plan on wiping all related tables).
R::exec('SET FOREIGN_KEY_CHECKS = 0;');
$listOfTables = R::$writer->getTables();
foreach($listOfTables as $table)
{
R::wipe($table);
}
R::exec('SET FOREIGN_KEY_CHECKS = 1;');

Strange constraints in generated SQL

Note: This question is not related to Visual Paradigm. Anyone that knows SQL could answer it.
I am using Visual Paradigm to model a database in our project (using ER diagrams). When Visual Paradigm generates the SQL equivalent for the database and I import it in MSSQL it works pretty.
I took a look in generated SQL code to make sure anything is right and I saw something strange!:
For tblContracts I defined a constraint named EndAfterStart to make sure the value of endDate is always bigger than startDate. The generated SQL code for this constraint is here:
IF NOT EXISTS (SELECT * FROM sys.check_constraints WHERE object_id=OBJECT_ID(N'[dbo].[EndAfterStart]'))
ALTER TABLE [dbo].[tblContracts] WITH CHECK ADD CONSTRAINT [EndAfterStart] CHECK (([startDate]<=[endDate]))
GO
ALTER TABLE [dbo].[tblContracts] CHECK CONSTRAINT [EndAfterStart]
GO
And the questions:
Why tblContracts is altered twice to add this constraint?!
Isn't first two lines enough?
What is different between second line and forth line?
First and second lines create EndAfterStart constraint if it doesn't exist. Fourth line enables EndAfterStart constraint.
The second line adds the constraint to the table; the fourth line enables the constraint.

SchemaExport, NHibernate and deleting foreign keys

I am building my mapping and then using schema export to update my DB. However, if I delete an association in my mapping, since it's no longer in the mapping, when I run SchemaExport, it will not delete the foreign key for the deleted association. This means that it then fails to drop the table associated with that foreign key. Which further means that it can't recreate the table and I get a "There is already an object named Foo in the database" exception. Is there any way to brute delete the table via Schema Export?
The cleanest way is to do SchemaExport.Drop with the old nhibernate configuration, then create with the new one.
Alternatively you could drop and recreate the database itself, here's an example which does this at file level for SQL Server Express: http://nicholas.piasecki.name/blog/2010/01/integration-testing-with-sql-server-express-2008-nhibernate-and-mstest/