SQLite Foreign Key

You still have to create the column checklist_id INTEGER before you add it as a Foreign key. So it would be: CREATE TABLE checklist ( _id INTEGER PRIMARY KEY AUTOINCREMENT, checklist_title TEXT, description TEXT, created_on INTEGER, modified_on INTEGER ); CREATE TABLE item ( _id INTEGER PRIMARY KEY AUTOINCREMENT, checklist_id INTEGER, item_text TEXT, item_hint TEXT, item_order … Read more

The better way to pass the foreign_key value to the Rails controller

You may want to consider reading the Rails Guide on nested resources: http://guides.rubyonrails.org/routing.html#nested-resources In a nutshell: routes.rb resources :galleries do resources :pictures do end # Generates the routes: /galleries/:gallery_id/pictures pictures_controller.rb def new @gallery = Gallery.find(params[:gallery_id]) @picture = Picture.new end def create @gallery = Gallery.find(params[:gallery_id]) # gallery_id is passed in the URL @picture = @gallery.build(params[:picture]) if … Read more

Django foreign key relation in template

If you review the foreign key documentation, if you have a relationship like Doc -> has many DocImages you need to define your foreign key on the DocImages class like so: class DocImage(models.Model): property = models.ForeignKey(Doc, related_name=”images”) If you don’t set related names, you can access the DocImages from the Doc like: Doc.docimage_set.all() Docs on … Read more

SQL Sub queries in check constraint

It is not supported to look beyond the current row in a CHECK constraint. http://www.postgresql.org/docs/9.1/interactive/sql-createtable.html says: A check constraint specified as a column constraint should reference that column’s value only, while an expression appearing in a table constraint can reference multiple columns. Currently, CHECK expressions cannot contain subqueries nor refer to variables other than columns … Read more

Proper database model for a user feedback system (an interesting case)

This is a bad design. Just make a 2-column primary key, and 2-column foreign keys to it. This is a fundamental anti-pattern called “encoding information in keys” which (thereby) are called “smart”, “intelligent” or “concatenated” keys. A good key is a “dumb” key. Eg:: Despite it now being easy to implement a Smart Key, it … Read more

Foreign key relationship with composite primary keys in SQL Server 2005

Since Table2 has a composite primary key (FileID, FileType), then any reference to it must also include both columns. ALTER TABLE dbo.Table1 ADD CONSTRAINT FK_Table1_Table2 FOREIGN KEY(FileID, FileType) REFERENCES Table2(FileID, FileType) Unless you have a unique constraint/index on the Table2.FileID field (but if so: why isn’t this the PK??), you cannot create a FK relationship … Read more

Defining multiple Foreign Key for the Same table in Entity Framework Code First

To achieve what you want you need to provide some aditional configuration.Code First convention can identify bidirectional relationships, but not when there are multiple bidirectional relationships between two entities.You can add configuration (using Data Annotations or the Fluent API) to present this information to the model builder. With Data Annotations, you’ll use an annotation called … Read more