I get that foreign key constraints great for integrity of a database and all, but it's also a huge overhead to use constraints when dealing with tables that are in the millions and growing.
I want to remove foreign key constraints from my application. In past symfony2 projects I've removed constraints manually, but I'm assuming maybe there is a way to simply tell symfony2 to do this?
If anyone is aware of a way to do this within the framework please let me know :)
edit:
Let's say for example in a manyToMany relationship, it auto-creates the relationship table with the proper indexes but it also puts a foreign key constraint on both columns as well, or if i have a oneToMany relationship it puts a foreign key constraint on that relationship.
I don't want these foreign key constraints to be created.
Found in Doctrine's JIRA:
You can disable the exporting of foreign keys for specific models:
User:
attributes:
export: tables
columns:
or with php:
$userTable->setAttribute(Doctrine::ATTR_EXPORT, Doctrine::EXPORT_TABLES);
Now it will only export the table definition and none of the foreign keys. You can use: none, tables, constraints, plugins, or all.
Related
Per the documents, one can instruct Doctrine to ignore one or more tables so that the migration tools will not complain about the database being out of sync. How can one instruct Doctrine's migration tools to ignore a foreign key constraint?
Reason for asking is that I will have a Doctrine ignored table with its primary key containing allowed values for a given column in a Doctrine managed table, and I wish to keep the foreign key in the Doctrine managed table back to the non-Doctrine managed constraints table.
I have question about making relation between two tables in mysql. I create table with column which is foreign key, but I dont use foreign key references keyword. I connect tables in code(php/asp.net). I dont know if it is good habit? Thanks for your help.
It's generally seen as a good habit to create a foreign key constraint as it will enforce data integrity between the two columns.
Yes, you can have 100% valid data in your database without using any foreign key constraints at all, but implementing them will make it impossible for a flawed update, delete, or insert to violate the foreign constraint between them in the future.
At past, I was used to make a table relationship programmatically, which is quite handy since you don't need to make FK constraint to each table which have relation.
But, I wonder what is the differences or the advantages of giving a FK constraint to tables that have relation, instead of just creating an attribute and retrieve them programmatically (calls the tables where field = another table PK).
Just some information, I work on php independent MVC framework without any dependency to eloquent or something else.
Hope someone give me some short lesson on this :D Thank you and have a nice day!
There are certain principles that you should follow while coding and development, I can say that there is no issue whether or not you create a foreign key constraint to a table that has relation or not but you know that won't restrict the column to have only those values that are being referenced by it. So basically it is not a good DB Schema and may lead to inconsistencies. For example deleting a parent table's row you will have to manually delete the child table's row on the other hand if you have a foreign key constraint that to onDelete = cascade, your database will automatically take care of everything and there won't any inconsistencies.
I am currently doing ZF2 project with Doctrine2. I have existing database where tables does not have foreign key constraints and any relation. Using doctrine can I generate schema based on relation without foreign key constraints.
I tried to add the column without foreign key constraints and its violated. Also I tried the column null able still doctrine try to modify the db schema to add foreign key constraints which eventually fails.
Also tried to add another temporary column as foreign key and later on write a script top transfer existing column to the foreign key constraint column. But it also failed as there have some columns who does not exist in the second table( may be deleted)
still does not find any suitable way to do that.
Any idea? or its not possible?
I'm using doctrine2 with a symfony2.1 project. I have an entity that has a few many to one relationships to other tables. The foreign key relationships for these many-to-one's have already been updated in the database, but every time I run migrations:diff or schema:update --dump-sql it adds the same update commands to add the foreign key relationships again. When I run schema:validate it says my mapping is out of sync with my database.
My application works fine, the relationships are working properly, and the schema in my database looks correct. Why is doctrine still trying to add these foreign keys?
Here's my code (for one of the problematic parameters):
In my "Ticket" entity I have:
/**
* Authenticated User who scored the ticket.
*
* #ORM\ManyToOne(targetEntity="CS\SecurityBundle\Entity\User")
* #ORM\JoinColumn(name="scoring_user_id", referencedColumnName="id")
*/
protected $scoringUser;
I currently have it set up to be one-directional, so there is no inversedBy in the User entity.
This generates the following in my migrations or schema:update dump even though it's in the database already:
$this->addSql("ALTER TABLE tickets ADD CONSTRAINT FK_54469DF4BB0D9452 FOREIGN KEY (scoring_user_id) REFERENCES users (id)");
Any idea what I'm doing wrong here?
Why is doctrine still trying to add these foreign keys?
The correct term here is "foreign key constraint". Without the constraint, the column in question is just a column. It's the constraint that enforces that the value of that column exists as a primary key in another table.
Why is doctrine still trying to add these foreign keys?
Because the database vendor/engine doesn't support foreign key constraints, but Doctrine fails to recognize that.
If I have to guess, you're using MySQL with the MyISAM engine. MyISAM doesn't support foreign key constraints. Unfortunately Doctrine isn't "smart" enough to see that. It sees that MySQL is used, therefor blindly assumes that foreign key constraints are supported.
My advise is to switch to the InnoDB engine, unless you have a good reason for using MyISAM.
ALTER TABLE table_name ENGINE=InnoDB;
Converting Tables from MyISAM to InnoDB