Skip to content
This repository has been archived by the owner on Sep 30, 2024. It is now read-only.

Bug constructing Foreign Keys on ssc pull #39

Closed
1 task done
Jaxelr opened this issue Sep 6, 2018 · 4 comments
Closed
1 task done

Bug constructing Foreign Keys on ssc pull #39

Jaxelr opened this issue Sep 6, 2018 · 4 comments
Labels

Comments

@Jaxelr
Copy link
Contributor

Jaxelr commented Sep 6, 2018

Type

What kind of issue is this?

  • Bug report.

Current Behavior

Given the following tables as created by the script:

IF OBJECT_ID('Child') > 0  DROP TABLE Child
IF OBJECT_ID('Parent') > 0  DROP TABLE Parent

CREATE TABLE Parent
(
    ParentId INT IDENTITY(1,1) NOT NULL,
	Name NVARCHAR(128) NOT NULL
	,CONSTRAINT [PK_Parent] PRIMARY KEY CLUSTERED (ParentId ASC)
) ON [PRIMARY];


CREATE TABLE Child
(
    ChildId INT IDENTITY(1,1) NOT NULL,
	Name NVARCHAR(128) NOT NULL,
	ParentId INT NOT NULL
	,CONSTRAINT [PK_Child] PRIMARY KEY CLUSTERED (ChildId ASC)
) ON [PRIMARY];

ALTER TABLE Child ADD CONSTRAINT [FK_Child_Parent] FOREIGN KEY ([ParentId])
REFERENCES Parent (ParentId);

The command ssc pull incorrectly generates the foreign key association. This is the resulting script (catted for readability):

if not exists (select * from sys.schemas where name = 'dbo')
exec('create schema dbo')
go

if not exists (select * from sys.objects where object_id = object_id('[dbo].[Child]') and type = 'U')
create table [dbo].[Child]
(
    [ChildId] int not null identity(1, 1),
    [Name] nvarchar(128) collate SQL_Latin1_General_CP1_CI_AS not null,
    [ParentId] int not null,
    constraint [PK_Child] primary key ([ChildId] asc)
)
go

if not exists (select * from sys.objects where object_id = object_id('[dbo].[Parent]') and type = 'U')
create table [dbo].[Parent]
(
    [ParentId] int not null identity(1, 1),
    [Name] nvarchar(128) collate SQL_Latin1_General_CP1_CI_AS not null,
    constraint [PK_Parent] primary key ([ParentId] asc)
    *alter table [dbo].[Parent] with check add constraint [FK_Child_Parent] foreign key([ParentId]) references [dbo].[Parent] ([ParentId]) alter table [dbo].[Parent] check constraint [FK_Child_Parent]*
)
go

Expected behavior

I would expect the foreign key to be created on the correct table (Child table) instead of the Parent table. I would also point out that the order of the alter is incorrectly included inside the create table and it does not compile correctly.

Steps to Reproduce

  1. Execute the query included above.
  2. ssc init
  3. ssc pull

Other Information

  • Version 1.9.0 of ssc.
  • Tested on SQL Server 2014 Developer Edition.
  • Any other information i could give you simply ping me. I will try and take a look at the source once i get a chance.
@DanielOverdevest
Copy link

Experienced the same issue today!
Also interesting in a quick fix. Please keep me posted. Thanks.

@Jaxelr
Copy link
Contributor Author

Jaxelr commented Sep 14, 2018

Thanks for attending this Issue, is there any ETA on when can we expect this release on npm?

@justinlettau
Copy link
Owner

Just published 1.9.1! @Jaxelr Thanks for this fix!

@Jaxelr
Copy link
Contributor Author

Jaxelr commented Sep 14, 2018

Thanks for the quick reply 👍 💯

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

3 participants