[Nitro] OG automatically adding Foreign Key constraints

Rob Pitt rob at motionpath.com
Fri Dec 2 12:33:05 EST 2005


The problem was that the old patch never added constraints to anything 
except join tables. Not only does this new patch do that, it is much 
simpler and if you have :evolve_schema_cautious set to false, it will 
automatically set values that are corrupt and prevent the constraints 
being added (left around from old deleted objects) to NULL.

I have changed the naming scheme somewhat for the constraints. Since 
this is only really going to effect a few people and is unlikely to 
happen again I have not bloated Og out with code to clean this up. 
Instead, here is a script I knocked together (very quickly, I know it's 
horrible :)) that will strip a PostgreSQL database (or all PostgreSQL 
databases) on a system of foreign key constraints, then when you next 
re-launch Og it will re-add them.

This will not change the data only constraints. I suggest anyone who 
uses Og with PostgreSQL runs this script on a database before using this 
new patch with it. Do not randomly run it on all databases unless you 
know what you are doing. It is safe to use with any Og only database store.

It is interactive and will prompt you for which database to clean up. 
You need to fill out the options hash at the top of the script with the 
credentials for a database to connect to. Once connected, it will get a 
list of database available. This is *not* the database it will strip of 
constraints, the database here will not be changed.

You will need to

Rob Pitt wrote:

>Please send me the smallest amount of code possible to reproduce this 
>problem (as a complete runnable nitro project) and I will fix it.
>
>Isaac Devine wrote:
>
>  
>
>>On Tue, 2005-11-29 at 14:47 +0000, Tim Larson wrote:
>> 
>>
>>    
>>
>>>On Tue, Nov 29, 2005 at 02:43:49PM +0000, Tim Larson wrote:
>>>   
>>>
>>>      
>>>
>>>>On Tue, Nov 29, 2005 at 03:47:26PM +1300, Isaac Devine wrote:
>>>>     
>>>>
>>>>        
>>>>
>>>>>When using belongs_to and has_many Og isn't generating Foreign Key
>>>>>Constraints :
>>>>>
>>>>>class Example1
>>>>>property :name, String
>>>>>has_many :children, Example2
>>>>>end
>>>>>
>>>>>class Example2
>>>>>property :address, String
>>>>>belongs_to :parent, Example1
>>>>>end
>>>>>
>>>>>What am I doing wrong? 
>>>>>Failing that is there a way I specify the constraints?
>>>>>       
>>>>>
>>>>>          
>>>>>
>>>>Because the relation name ":children" does not match the name of
>>>>the related class "Example2" Og is getting lost...change your
>>>>"has_many" line like this to give it the clue it needs:
>>>> has_many :children, Example2, :foreign_name => :parent
>>>>     
>>>>
>>>>        
>>>>
>>>I think my explanation was wrong...the problem is because the name of
>>>relation ":children" does not match the name used in the belongs_to
>>>relation ":parent", but you probably figured that out from the example
>>>has_many line above.
>>>
>>>   
>>>
>>>      
>>>
>>Still doesn't work. FWIW I am using postgres and the Debug output is as
>>follows (added a line to output the generated sql in psql.rb):
>>
>>D, [2005-11-30T08:46:30.383009 #6720] DEBUG -- : Og manageable classes:
>>[Example2, Example1]
>>D, [2005-11-30T08:46:30.408267 #6720] DEBUG -- : Generated sql:
>>CREATE TABLE ogexample2 (parent_oid integer, address text, oid serial
>>PRIMARY KEY) WITHOUT OIDS;
>>NOTICE:  CREATE TABLE will create implicit sequence "ogexample2_oid_seq"
>>for "serial" column "ogexample2.oid"
>>NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index
>>"ogexample2_pkey" for table "ogexample2"
>>I, [2005-11-30T08:46:30.456938 #6720]  INFO -- : Created table
>>'ogexample2'.
>>D, [2005-11-30T08:46:30.479274 #6720] DEBUG -- : Generated sql:
>>CREATE TABLE ogexample1 (name text, oid serial PRIMARY KEY) WITHOUT
>>OIDS;
>>NOTICE:  CREATE TABLE will create implicit sequence "ogexample1_oid_seq"
>>for "serial" column "ogexample1.oid"
>>NOTICE:  CREATE TABLE / PRIMARY KEY will create implicit index
>>"ogexample1_pkey" for table "ogexample1"
>>I, [2005-11-30T08:46:30.493831 #6720]  INFO -- : Created table
>>'ogexample1'.
>>
>>thanks,
>>Isaac
>>
>>_______________________________________________
>>Nitro-general mailing list
>>Nitro-general at rubyforge.org
>>http://rubyforge.org/mailman/listinfo/nitro-general
>> 
>>
>>    
>>
>
>_______________________________________________
>Nitro-general mailing list
>Nitro-general at rubyforge.org
>http://rubyforge.org/mailman/listinfo/nitro-general
>  
>

-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: kill-constraints.rb
Url: http://rubyforge.org/pipermail/nitro-general/attachments/20051202/43a141a6/attachment.pl 
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: nitro.patch
Url: http://rubyforge.org/pipermail/nitro-general/attachments/20051202/43a141a6/attachment-0001.pl 


More information about the Nitro-general mailing list