Why do Rails migrations define foreign keys in the application but not in the database?

Rails holds some conventions that enforcement of data integrity should be done in the application, not in the database. For example, Rails even supports some database designs that cannot use foreign keys, such as Polymorphic Associations. Basically, Rails conventions have treated the database as a static data storage device, not an active RDBMS. Rails 2.0 … Read more

How do I disable referential integrity in Postgres 8.2?

There are two things you can do (these are complementary, not alternatives): Create your foreign key constraints as DEFERRABLE. Then, call “SET CONSTRAINTS DEFERRED;”, which will cause foreign key constraints not to be checked until the end of the transaction. Note that the default if you don’t specify anything is NOT DEFERRABLE (annoyingly). Call “ALTER … Read more

Rails: delete cascade vs dependent destroy

It really depends on the behavior you want. In case 1, destroy will be called on each associated order, and therefor so will the ActiveRecord callbacks. In case 2, these callbacks are not triggered, but it will be way faster and guarantees referential integrity. In an application’s infancy, I’d recommend going with :dependent => :destroy … Read more

CONSTRAINT to check values from a remotely related table (via join etc.)

CHECK constraints cannot currently reference other tables. The manual: Currently, CHECK expressions cannot contain subqueries nor refer to variables other than columns of the current row. One way is to use a trigger like demonstrated by @Wolph. A clean solution without triggers: add redundant columns and include them in FOREIGN KEY constraints, which are the … Read more

In MySQL, can I defer referential integrity checks until commit

Looks like my answer is here… Like MySQL in general, in an SQL statement that inserts, deletes, or updates many rows, InnoDB checks UNIQUE and FOREIGN KEY constraints row-by-row. When performing foreign key checks, InnoDB sets shared row-level locks on child or parent records it has to look at. InnoDB checks foreign key constraints immediately; … Read more

MySQL foreign key to allow NULL?

You can solve this by allowing NULL in the foreign key column tblImageFlags.resolutionTypeID. PS Bonus points to whomever tells me whether, in the case of databases, it’s “indexes” or “indices”. The plural of index should be indexes. According to “Modern American Usage” by Bryan A. Garner: For ordinary purposes, indexes is the preferable plural, not … Read more

What’s wrong with foreign keys?

Reasons to use Foreign Keys: you won’t get Orphaned Rows you can get nice “on delete cascade” behavior, automatically cleaning up tables knowing about the relationships between tables in the database helps the Optimizer plan your queries for most efficient execution, since it is able to get better estimates on join cardinality. FKs give a … Read more

Create unique constraint with null columns

Postgres 15 or newer Postgres 15 (currently beta) adds the clause NULLS NOT DISTINCT. The release notes: Allow unique constraints and indexes to treat NULL values as not distinct (Peter Eisentraut) Previously NULL values were always indexed as distinct values, but this can now be changed by creating constraints and indexes using UNIQUE NULLS NOT … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)