Skip to content

tSQLt.ApplyConstraint fails when constraint's name must be quoted #163

Open
@CodingForFunToo

Description

@CodingForFunToo

When a constraint needs quotes because it contains punctuations or spaces, tSQLt.ApplyConstraint fails because it passes parameters @SchemaName and @OriginalName without quotes to procedure tSQLt.Private_MarkObjectBeforeRename. Within this proc OBJECT_ID(@SchemaName + '.' + @OriginalName) evaluates to NULL.

T-SQL Code to reproduce:

create table dbo.MasterTbl(id int not null identity constraint [PK_Master] primary key clustered);

create table dbo.ChildTbl
(
  id int not null identity constraint [PK_Child] primary key clustered
, masterId int not null constraint [FK.with.dots and spaces] foreign key references dbo.MasterTbl(id)
);

EXEC tSQLt.NewTestClass @ClassName = N'ConstraintTests';

CREATE PROCEDURE ConstraintTests.[test that fails to execute]
as
begin
exec tSQLt.FakeTable @TableName       = N'dbo.MasterTbl';
exec tSQLt.FakeTable @TableName       = N'dbo.ChildTbl';
exec tSQLt.ApplyConstraint @TableName = N'dbo.ChildTbl', @ConstraintName = N'FK.with.dots and spaces';

exec tSQLt.Fail @Message0 = N'Will never be executed, because tSQLt.ApplyConstraint fails';
end
GO

exec tSQLt.Run @TestName = N'ConstraintTests';

exec tSQLt.DropClass @ClassName = N'ConstraintTests';
drop table dbo.ChildTbl;
drop table dbo.MasterTbl;

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions