# Expressing a Complex DDL in Rails

**URL:** <https://www.ruby-forum.com/t/expressing-a-complex-ddl-in-rails/109493>\
**Category:** Rails\
**Created:** [July 15, 2007, 9:05pm UTC](https://www.ruby-forum.com/t/expressing-a-complex-ddl-in-rails/109493 "2007-07-15T21:05:16Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![lrojas](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/90ced4/32.png) [@lrojas](https://www.ruby-forum.com/u/lrojas)\
**Post date:** [July 15, 2007, 9:05pm UTC](https://www.ruby-forum.com/t/expressing-a-complex-ddl-in-rails/109493/1 "2007-07-15T21:05:16Z")

</div>

Hi all,

i am migrating an aplication to Rails and it has some weird construct  
to handle References and DropDown Values, i was wondering how could i  
express this in Rails? i put it on [Parked at Loopia](http://pastie.caboo.se/79013) but in  
case it expires here is the DDL

SET ANSI\_NULLS ON  
GO  
SET QUOTED\_IDENTIFIER ON  
GO  
IF NOT EXISTS (SELECT \* FROM sys.objects WHERE object\_id =  
OBJECT\_ID(N’[dbo].[ReferenciasTipos\_T]‘) AND type in (N’U’))  
BEGIN  
CREATE TABLE [dbo].[ReferenciasTipos\_T](  
[TipID] [int] IDENTITY(1,1) NOT NULL,  
[TipNombre] nvarchar NOT NULL,  
[TipDescripcion] nvarchar NULL,  
[TipIsNotas] char NOT NULL CONSTRAINT  
[DF\_ReferenciasTipos\_T\_TipIsNotas] DEFAULT (‘N’),  
[TipTituloNotas] nvarchar NULL,  
[TipTituloNotaUnaLinea] nvarchar NULL,  
[TipIsNotaUnaLinea] char NOT NULL CONSTRAINT  
[DF\_ReferenciasTipos\_T\_TipIsNotaUnaLinea] DEFAULT (‘N’),  
[TipPadreID] [int] NULL,  
[TipSystemUser] char NULL,  
[Creacion] [datetime] NOT NULL CONSTRAINT  
[DF\_ReferenciasTipos\_T\_Creado] DEFAULT (getdate()),  
[Modificacion] [datetime] NOT NULL CONSTRAINT  
[DF\_ReferenciasTipos\_T\_Modificado] DEFAULT (getdate()),  
[Creacion\_UsrID] [int] NULL,  
[DeletedDate] [datetime] NULL,  
CONSTRAINT [PK\_ReferenciasTipos\_T] PRIMARY KEY CLUSTERED  
(  
[TipID] ASC  
)WITH (IGNORE\_DUP\_KEY = OFF) ON [PRIMARY]  
) ON [PRIMARY]  
END  
GO  
SET ANSI\_NULLS ON  
GO  
SET QUOTED\_IDENTIFIER ON  
GO  
IF NOT EXISTS (SELECT \* FROM sys.objects WHERE object\_id =  
OBJECT\_ID(N’[dbo].[ReferenciasValores\_T]‘) AND type in (N’U’))  
BEGIN  
CREATE TABLE [dbo].[ReferenciasValores\_T](  
[ValorID] [int] IDENTITY(1,1) NOT NULL,  
[TipID] [int] NOT NULL,  
[ParentID] [int] NULL,  
[Descripcion] nvarchar NOT NULL,  
[Orden] [int] NULL CONSTRAINT [DF\_ReferenciasValores\_T\_Orden]  
DEFAULT ((0)),  
[Notas] [text] NULL,  
[NotaUnaLinea] nvarchar NULL,  
[Creacion] [datetime] NOT NULL CONSTRAINT  
[DF\_ReverenciasValores\_Creado] DEFAULT (getdate()),  
[Modificacion] [datetime] NOT NULL CONSTRAINT  
[DF\_ReverenciasValores\_Modificado] DEFAULT (getdate()),  
[Creacion\_UsrID] [int] NULL,  
[Comp\_ID] [int] NULL,  
[DeletedDate] [datetime] NULL,  
CONSTRAINT [PK\_ReverenciasValores] PRIMARY KEY CLUSTERED  
(  
[ValorID] ASC  
)WITH (IGNORE\_DUP\_KEY = OFF) ON [PRIMARY]  
) ON [PRIMARY] TEXTIMAGE\_ON [PRIMARY]  
END  
GO  
IF NOT EXISTS (SELECT \* FROM sys.foreign\_keys WHERE object\_id =  
OBJECT\_ID(N’[dbo].[FK\_ReferenciasTipos\_T\_ReferenciasTipos\_T]‘) AND  
parent\_object\_id = OBJECT\_ID(N’[dbo].[ReferenciasTipos\_T]‘))  
ALTER TABLE [dbo].[ReferenciasTipos\_T] WITH CHECK ADD CONSTRAINT  
[FK\_ReferenciasTipos\_T\_ReferenciasTipos\_T] FOREIGN KEY([TipPadreID])  
REFERENCES [dbo].[ReferenciasTipos\_T] ([TipID])  
GO  
IF NOT EXISTS (SELECT \* FROM sys.foreign\_keys WHERE object\_id =  
OBJECT\_ID(N’[dbo].[FK\_ReferenciasValores\_T\_Usuarios\_T]‘) AND  
parent\_object\_id = OBJECT\_ID(N’[dbo].[ReferenciasValores\_T]‘))  
ALTER TABLE [dbo].[ReferenciasValores\_T] WITH CHECK ADD CONSTRAINT  
[FK\_ReferenciasValores\_T\_Usuarios\_T] FOREIGN KEY([Creacion\_UsrID])  
REFERENCES [dbo].[Usuarios\_T] ([UsrID])  
GO  
IF NOT EXISTS (SELECT \* FROM sys.foreign\_keys WHERE object\_id =  
OBJECT\_ID(N’[dbo].[FK\_ReverenciasValores\_ReferenciasTipos\_T]‘) AND  
parent\_object\_id = OBJECT\_ID(N’[dbo].[ReferenciasValores\_T]‘))  
ALTER TABLE [dbo].[ReferenciasValores\_T] WITH CHECK ADD CONSTRAINT  
[FK\_ReverenciasValores\_ReferenciasTipos\_T] FOREIGN KEY([TipID])  
REFERENCES [dbo].[ReferenciasTipos\_T] ([TipID])  
GO  
IF NOT EXISTS (SELECT \* FROM sys.foreign\_keys WHERE object\_id =  
OBJECT\_ID(N’[dbo].[FK\_ReverenciasValores\_ReverenciasValores]‘) AND  
parent\_object\_id = OBJECT\_ID(N’[dbo].[ReferenciasValores\_T]'))  
ALTER TABLE [dbo].[ReferenciasValores\_T] WITH CHECK ADD CONSTRAINT  
[FK\_ReverenciasValores\_ReverenciasValores] FOREIGN KEY([ParentID])  
REFERENCES [dbo].[ReferenciasValores\_T] ([ValorID])
