[ruby-it] le foreign keys.

Paolo Montrasio paolo a paolomontrasio.com
Mer 17 Dic 2008 09:19:36 CET


Mauro wrote:
>     create_table :line_items do |t|
>       t.integer :product_id,  :null => false, :options => "CONSTRAINT
> fk_line_item_products REFERENCES products(id)"

Anche questa č una possibilitā, ma il libro prosegue avvertendo che 
"Rails migrations doesn’t provide a database-independent way to specify 
these foreign key constraints, so we had to resort to adding native DDL 
clauses". Quel codice in particolare č per SQLlite3.

Quando ritorna sull'esempio un centinaio di pagine dopo infatti crea 
anche lui un modulo MigrationHelper che definisce un metodo foreign_key. 
Il codice č totalmente diverso da quello che ho presentato io ma l'idea 
č la stessa.

L'ideale sarebbe avere un plugin con driver per i vari db, esattamente 
come fa ActiveRecord. Anzi, l'ideale sarebbe che 
ActiveRecord::ConnectionAdapters::TableDefinition finalmente supportasse 
nativamente le foreign key. Cito Agile Web Development with Rails:

"Many Rails developers don’t bother specifying database-level 
constraints such as foreign keys, relying instead on the application 
code to make sure that everything knits together correctly. That’s 
probably why Rails migrations don’t let you specify constraints. 
However, when it comes to database integrity, many (including Dave and 
Sam) think an ounce of extra checking can save pounds of latenight 
production system debugging."

Una cosa infatti č certa: prima o poi l'applicazione sbaglierā e lascerā 
dati inconsistenti nel database. Sta al db difendersi tramite 
l'impostazione degli opportuni constraint.

Paolo
-- 
Posted via http://www.ruby-forum.com/.


More information about the Ml mailing list