Skip to content

Unique index created /w SQLServerPlatform cannot be used for FK #5507

@mvorisek

Description

@mvorisek

Bug Report

Q A
Version 3.3.7

Summary

repro https://dbfiddle.uk/?rdbms=sqlserver_2019&fiddle=36b1193feaaab2d8f54cb5d7aadd32c8

source of the issue

return $sql . ' WHERE ' . implode(' AND ', $fields);

Current behaviour

FK constraint requires a foreign column to be unique. This is standard requirement across the DB vendors.

However SQL Server requires null value to be unique as well. This is atypicial for unique index and DBAL overcomes this behaviour by adding WHERE xxx IS NOT NULL to the unique index definition.

This extra condition on the unique index causes FK cannot be created. SQL Server is not smart enough to understand the IS NOT NULL is always satisfied with not nullable column.

How to reproduce

see links in the summary

Expected behaviour

The expected behaviour is unique index is created without any WHERE when the target column does not allow null values.

Only then FK can be created.

repro without WHERE xxx IS NOT NULL - FK is successfully added - https://dbfiddle.uk/?rdbms=sqlserver_2019&fiddle=78d464488260d735f4d15aa42ae6f8e5

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