foreign-key-relationship

MySQL 5.5 foreign key constraint fails when foreign key exists

久未见 提交于 2019-11-26 14:37:42
问题 Just installed MySQL 5.5 on mac os x 10.6 and am having a strange issue on many tables. Below is an example. Inserting a row fails with a foreign key constraint when it shouldn't. The foreign key it references does exist. Any ideas? mysql> show create table Language; +----------+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

SQL Sub queries in check constraint

有些话、适合烂在心里 提交于 2019-11-26 14:18:41
问题 Can I make SQL sub queries in Check constraint ? I've a post table with columns id, owner I've another table action with columns user_id, post_id Table user with columns id post_id -> post.id and user_id -> user.id also post.owner -> user.id Now I want to constraint post(post_id).id != user_id on table action How is that possible ? 回答1: 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

Why can you not have a foreign key in a polymorphic association?

核能气质少年 提交于 2019-11-26 09:44:24
Why can you not have a foreign key in a polymorphic association, such as the one represented below as a Rails model? class Comment < ActiveRecord::Base belongs_to :commentable, :polymorphic => true end class Article < ActiveRecord::Base has_many :comments, :as => :commentable end class Photo < ActiveRecord::Base has_many :comments, :as => :commentable #... end class Event < ActiveRecord::Base has_many :comments, :as => :commentable end A foreign key must reference only one parent table. This is fundamental to both SQL syntax, and relational theory. A Polymorphic Association is when a given

When to use “ON UPDATE CASCADE”

烂漫一生 提交于 2019-11-26 05:38:04
I use "ON DELETE CASCADE" regularly but I never use "ON UPDATE CASCADE" as I am not so sure in what situation it will be useful. For the sake of discussion let see some code. CREATE TABLE parent ( id INT NOT NULL AUTO_INCREMENT, PRIMARY KEY (id) ); CREATE TABLE child ( id INT NOT NULL AUTO_INCREMENT, parent_id INT, INDEX par_ind (parent_id), FOREIGN KEY (parent_id) REFERENCES parent(id) ON DELETE CASCADE ); For "ON DELETE CASCADE", if a parent with an id is deleted, a record in child with parent_id = parent.id will be automatically deleted. This should be no problem. This means that "ON UPDATE

Why can you not have a foreign key in a polymorphic association?

坚强是说给别人听的谎言 提交于 2019-11-26 02:04:35
问题 Why can you not have a foreign key in a polymorphic association, such as the one represented below as a Rails model? class Comment < ActiveRecord::Base belongs_to :commentable, :polymorphic => true end class Article < ActiveRecord::Base has_many :comments, :as => :commentable end class Photo < ActiveRecord::Base has_many :comments, :as => :commentable #... end class Event < ActiveRecord::Base has_many :comments, :as => :commentable end 回答1: A foreign key must reference only one parent table.

When to use “ON UPDATE CASCADE”

被刻印的时光 ゝ 提交于 2019-11-26 01:50:27
问题 I use \"ON DELETE CASCADE\" regularly but I never use \"ON UPDATE CASCADE\" as I am not so sure in what situation it will be useful. For the sake of discussion let see some code. CREATE TABLE parent ( id INT NOT NULL AUTO_INCREMENT, PRIMARY KEY (id) ); CREATE TABLE child ( id INT NOT NULL AUTO_INCREMENT, parent_id INT, INDEX par_ind (parent_id), FOREIGN KEY (parent_id) REFERENCES parent(id) ON DELETE CASCADE ); For \"ON DELETE CASCADE\", if a parent with an id is deleted, a record in child

What is `related_name` used for in Django?

两盒软妹~` 提交于 2019-11-26 00:26:45
问题 What is the related_name argument useful for on ManyToManyField and ForeignKey fields? For example, given the following code, what is the effect of related_name=\'maps\' ? class Map(db.Model): members = models.ManyToManyField(User, related_name=\'maps\', verbose_name=_(\'members\')) 回答1: The related_name attribute specifies the name of the reverse relation from the User model back to your model. If you don't specify a related_name , Django automatically creates one using the name of your