Em 28-11-2011 14:01, Mislav Marohnić escreveu:
There are 3 main relational databases that Rails devs care about: SQLite, MySQL
and Postgres.
SQLite hasn't had foreign key constraints until late 2009 and MySQL only has it
on InnoDB tables (not default). So, for the most of Rails' lifetime, FKC
support in supported databases wasn't ubiquitous. When you think about it, it
makes sense that AR chose to implement these checks in the application (via
validations) rather than relying on RDBMS.
It should not be a matter of validations vs foreign keys. Both should be
used. The validations will help dealing with some cases in a
user-friendly way.
Foreign keys, in the other side, enforces constraints in the database
level. It means that if your sysadmin needs to do some manual procedures
in the database, it won't leave it in an inconsistent state. Neither
another application using the same database but through a different ORM
solution or by using direct SQL statements.
The only big feature of relational systems that I can recall AR connected to is
stored procedures, and that is Postgres-only if I remember. It had benefits in
form of significant speedups. What significant gain would there be from relying
on FKC?
It would also prevent incorrect state to happen in the presence of some
bugs in the validation side.
It also helps documenting the database.
I mean, there is no excuse for not adding the foreign key in databases
where they are supported. While on the subject, I can't really
understand why MySql has so much popularity. It doesn't even support
foreign keys by default!
Rails should add the feature to the Migrations framework regardless of
it being supported or not by the database. In the case the database
doesn't support it, it should simply create an index and it should be
left to the developers the decision of using a good database like
PostgreSQL or a bad one like MySql (Sqlite3 is being used only for
development - or at least, it should, so it shouldn't matter that much).
But specially, as James Byrne has already pointed out, it leads to
inconsistency in the databases even if there is no bug in the
validations code due to race conditions.
If some user asks some data to be removed while another asked for some
item to be associated with the same data being removed, it could happen
that the created item would point out to an item that does not exist
anymore.
So, let me ask you the opposite. Why not supporting FKC?
--
You received this message because you are subscribed to the Google Groups "Ruby on
Rails: Core" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/rubyonrails-core?hl=en.