foreign-keys

Validations for a has_many/belongs_to relationship

I have a Recipe model which has_many Ingredients (which in turn belongs_to Recipe). I want Ingredient to be existent dependent on Recipe; an Ingredient should never exist without a Recipe. I'm trying to enforce the presence of a valid Recipe ID in the Ingredient. I've been doing this with a validates :recipe, :presence => true (Rails 3)...

Is it possible to alter the cascading behaviour of existing mysql foreign keys?

I have two tables in mysql, both are type InnoDB and there is an existing foreign key from the first to the second. However, the foreign key was created without any cascading behaviour. The table is large, and any changes to indexes or new keys locks the table for ~20 minutes. Is it possible to add "ON DELETE CASCADE" behaviour to an ...

MySQL foreign key constraints, cascade delete

Hello. I want to use foreign keys to keep the integrity and avoid orphans (I already use innoDB). How do I make a SQL statment that DELETE ON CASCADE? If I delete a category then how do I make sure that it would not delete products that also are related to other categories. The pivot table "categories_products" creates a many-to-many...

Can I use foreign key restrictions to return meaningful UI errors with PHP

I want to start by saying that I am a big fan of using foreign keys and have a tendency to use them even on small projects to keep my database from being filled with orphaned data. On larger projects I end up with gobs of keys which end up covering upwards of 8 - 10 layers of data. I want to know if anyone could suggest a graceful way ...

django i can't save form with 2 foreign keys

i have a Reply class: class Reply(models.Model): reply_to = models.ForeignKey(New) creator = models.ForeignKey(User) reply = models.CharField(max_length=140,blank=False) a replay form: class ReplyForm(ModelForm): class Meta: model = Reply fields = ['reply'] where New is the Post class (containing users posts...

Django blog reply system

hello, i'm trying to build a mini reply system, based on the user's posts on a mini blog. Every post has a link named reply. if one presses reply, the reply form appears, and one edits the reply, and submits the form.The problem is that i don't know how to take the id of the post i want to reply to. In the view, if i use as a parameter o...

T-SQL foreign key check constraint

When you create a foreign key constraint in a table and you create the script in MS SQL Management Studio, it looks like this. ALTER TABLE T1 WITH CHECK ADD CONSTRAINT FK_T1 FOREIGN KEY(project_id) REFERENCES T2 (project_id) GO ALTER TABLE T1 CHECK CONSTRAINT FK_T1 GO What I don't understand is what purpose has the second alter with...

MySQL Storage Engine supporting optionally indexed FKs

One of the more annoying things about InnoDB is that although it supports foreign keys it requires that foreign key columns in the referencing table be indexed. While that's often a good idea it certainly isn't always one. Take for example an orders table with ~20 columns. Aside from the single column primary key and perhaps the cre...

Sort queryset by a generic foreign key (django)?

I am using Django's comment framework which utilizes generic foreign keys. Question: How do I sort a given model's queryset by their comment count using the generic foreign key lookup? Reading the django docs on the subject it says one needs to calculate them not using the aggregation API: Django's database aggregation API doesn'...

Is it Possible to Use Constraints on Hierarchical Data in a Self-Referential Table?

Suppose you have the following table, intended to represent hierarchical data: +--------+-------------+ | Field | Type | +--------+-------------+ | id | int(10) | | parent | int(10) | | name | varchar(45) | +--------+-------------+ The table is self-referential in that the parent_id refers to id. So you might ha...

How can I create a small relational database in MySQL?

I need to make a small database in MySQL that has two tables. Clothes and ClotheType Clothes has: ID, Name, Color, Brand, Price, ClotheTypeID ClotheType has: ID, Description In Microsoft SQL it would be: create table Clothes( id int, primary key(id), name varchar(200), color varchar(200), brand varchar(200), price varchar(200), cloth...

MySQL - are FK's useful / viable in a web app?

Hi all, I've encountered this discussion related to FK's and web applications. Basically some people say that FK's in web applications doesn't represent a real improvement and can even make the application slower in some cases. What do you guys think, what's your experience? edit : note that I'm aware of FK's job and objective, I'm ju...

SQL - Two foreign keys that have a dependency between them

The current structure is as follows: Table RowType: RowTypeID Table RowSubType: RowSubTypeID FK_RowTypeID Table ColumnDef: FK_RowTypeID FK_RowSubTypeID (nullable) In short, I'm mapping column definitions to rows. In some cases, those rows have subtype(s), which will have column definitions sp...

django-admin formfield_for_* change default value per/depending on instance

Hi, I'm trying to change the default value of a foreignkey-formfield to set a Value of an other model depending on the logged in user. But I'm racking my brain on it... This: Changing ForeignKey’s defaults in admin site would an option to change the empty_label, but I need the default_value. #Now I tried the following without errors b...

PostgreSQL: How to index all foreign keys?

I am working with a large PostgreSQL database, and I am trying to tune it to get more performance. Our queries and updates seem to be doing a lot of lookups using foreign keys. What I would like is a relatively simple way to add Indexes to all of our foreign keys without having to go through every table (~140) and doing it manually. ...

Getting Hibernate to generate indexes on foreign keys

I am using Hibernate 3.3.2 in a fairly large project with PostgreSQL 8.4 using the PostgreSQLDialect. The way we have our relationships set up, we end up doing a lot of searching on the Foreign Key attributes of our tables. For performance reasons, I would like to have Hibernate add Indexes to the all of the foreign key columns when ...

is there any tools could help to convert innoDB table into NDB table for mysql cluster?

is there any tools could help to convert innoDB table into NDB table? I want a tool help to replace all foreign key by trigger. Should I have to do it by manual for each FK? ...

how to Insert null value in foreign key column

Hi I have two tables like x and y. x table have one PK column and y table have FK constraint of table x (with null). i need to insert null value in table y by passing null value. is it possible in NHibernate. i tried by passing null value and is throughing FK constraint error. Plz suggest me. Regards Jcreddy. ...

foreign key and index issue

Hello everyone, I am using SQL Server 2008 Enterprise. I have a table and one of its column is referring to another column in another table (in the same database) as foreign key, here is the related SQL statement, in more details, column [AnotherID] in table [Foo] refers to another table [Goo]'s column [GID] as foreign key. [GID] is pri...

SQL - Dervied Foreign Key - Possible?

I'm just curious if this is possible, specifically in SQL CE (Express) with support in .NET's Entity Framework: Table1 (primary) -nvarchar(2000) url -... Table2 (with foreign key) -nvarchar(2000) domain -... foreign key on Table2.domain references Table1.url such that Table.url contains Table2.domain e.g. Table1: http://www.google....