Reverse engineering FKs fails on MSSQL #462

Open
jdgrieco opened this Issue Sep 7, 2012 · 2 comments

Comments

Projects
None yet
3 participants
@jdgrieco

jdgrieco commented Sep 7, 2012

I recently started to use Propel and I think I found a bug:

Reverse engineering on MSSQL database via dblib doesn't add FK name in foreign-key tag
and a table with 2 or more FKs to different tables is incorrect mapped:

CREATE TABLE [dbo].[purchase_request_items](
    [id] [int] IDENTITY(1,1) NOT NULL,
    [purchase_request_id] [int] NULL,
    [product_id] [varchar](10) NULL,
 CONSTRAINT [PK_purchase_request_items] PRIMARY KEY CLUSTERED 
(
    [id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO

ALTER TABLE [dbo].[purchase_request_items]  WITH CHECK ADD  CONSTRAINT [FK_purchase_request_items_to_products] FOREIGN KEY([product_id])
REFERENCES [dbo].[products] ([product_id])
GO

ALTER TABLE [dbo].[purchase_request_items] CHECK CONSTRAINT [FK_purchase_request_items_to_products]
GO

ALTER TABLE [dbo].[purchase_request_items]  WITH CHECK ADD  CONSTRAINT [FK_purchase_request_items_to_purchase_requests] FOREIGN KEY([purchase_request_id])
REFERENCES [dbo].[purchase_requests] ([purchase_request_id])
ON UPDATE CASCADE
ON DELETE CASCADE
GO

ALTER TABLE [dbo].[purchase_request_items] CHECK CONSTRAINT [FK_purchase_request_items_to_purchase_requests]
GO

is mapped to XML:

<foreign-key foreignTable="purchase_requests" name="">
      <reference local="purchase_request_id" foreign="purchase_request_id"/>
      <reference local="product_id" foreign="product_id"/>
</foreign-key>

and a correct XML shoud be:

<foreign-key foreignTable="purchase_requests" name="FK_purchase_request_items_to_purchase_requests">
    <reference local="purchase_request_id" foreign="purchase_request_id"/>
</foreign-key>
<foreign-key foreignTable="products" name="FK_purchase_request_items_to_products">
    <reference local="product_id" foreign="product_id"/>
</foreign-key>

So I modify MssqlSchemaParser's addForeignKeys method, see https://gist.github.com/3667854

This modification correct this issue, and, as side effect, migrations works too!

@willdurand

This comment has been minimized.

Show comment Hide comment
@willdurand

willdurand Nov 21, 2012

Member

Heya @jdgrieco, could you open a Pull Request with your patch?

Member

willdurand commented Nov 21, 2012

Heya @jdgrieco, could you open a Pull Request with your patch?

@jaugustin

This comment has been minimized.

Show comment Hide comment
@jaugustin

jaugustin Sep 4, 2014

Member

@jdgrieco @willdurand got the same issue today and fixed it ;)

the issue is here : https://github.com/propelorm/Propel/blob/master/generator/lib/reverse/mssql/MssqlSchemaParser.php#L182

the $name is undefined and never fetched.

I will provide a PR today

Member

jaugustin commented Sep 4, 2014

@jdgrieco @willdurand got the same issue today and fixed it ;)

the issue is here : https://github.com/propelorm/Propel/blob/master/generator/lib/reverse/mssql/MssqlSchemaParser.php#L182

the $name is undefined and never fetched.

I will provide a PR today

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment