propelorm / Propel

Current stable (and outdated and unmaintained) version of Propel - Please use v2 https://github.com/propelorm/Propel2 --
http://www.propelorm.org
MIT License
841 stars 414 forks source link

Reverse engineering FKs fails on MSSQL #462

Open jdgrieco opened 11 years ago

jdgrieco commented 11 years ago

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 commented 11 years ago

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

jaugustin commented 9 years ago

@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