Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Greater than in UniqueConstraint not work correctly. #339

Closed
NovinDeveloper opened this issue Jan 22, 2024 · 4 comments · Fixed by #349
Closed

Greater than in UniqueConstraint not work correctly. #339

NovinDeveloper opened this issue Jan 22, 2024 · 4 comments · Fixed by #349

Comments

@NovinDeveloper
Copy link

Software versions

  • Django: 4.2.9
  • mssql-django: 1.4
  • python: 3.9.2
  • SQL Server: 2019

Table schema and Model

class TestModel(models.Model):
    Column = models.BigIntegerField()

    class Meta:
        db_table = "TestModel"
        constraints = [
            models.UniqueConstraint(fields=("Column",),
                                    condition=Q(Column__gt=0),
                                    name='column_unique')
        ]

Problem description and steps to reproduce
When i add a new record (Column=5463254 ) with django orm in TestModel, the generated validate query by mssql is:

SELECT
	1 AS [_check] 
WHERE
	COALESCE (
		(
			5463254 > 0 
		AND
		CASE
				
				WHEN EXISTS ( SELECT TOP 1 1 AS [a] FROM [TestModel] U0 WHERE ( U0.[Column] = 5463254 AND U0.[Column] > 0 ) ) THEN
			TRUE ELSE FALSE 
			END = TRUE 
	),
TRUE 
	) = TRUE 

Error message/stack trace
This query not work in sql server and i get this error when run this query in sql server:

Msg 102, Level 15, State 1, Line 6
Incorrect syntax near '>'.
Msg 156, Level 15, State 1, Line 10
Incorrect syntax near the keyword 'THEN'.

@NovinDeveloper NovinDeveloper changed the title Greather than in UniqueConstraint not work correctly. Greater than in UniqueConstraint not work correctly. Jan 22, 2024
@mShan0
Copy link
Contributor

mShan0 commented Jan 22, 2024

Hi @NovinDeveloper, does this happen on 1.3?

@NovinDeveloper
Copy link
Author

Hi @mShan0
Yes, this problem happen in 1.3.
It looks like this problem not happen when i add multi fields in "fields", but i want add only one column to "fields".

models.UniqueConstraint(fields=("Column", "Column1"),
                                    condition=Q(Column__gt=0),
                                    name='column_unique')

@mShan0
Copy link
Contributor

mShan0 commented Jan 24, 2024

Thanks for the info. Will investigate further

@dauinsight
Copy link
Contributor

Hi @NovinDeveloper you can expect a fix for this issue in the next release. Thank you.

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

Successfully merging a pull request may close this issue.

3 participants