SQLAlchemy and mssql : how to create unique constraint with multiple null value - sql-server

I am working with SQLAlchemy and MS SQL server and I would like to create a unique constraint that allows multiple NULL value.
I know that MS SQL server does not ignore the null value and considers it as violation for the UNIQUE KEY.
I also know how to fix it with SQL code (see here)
But is there a way to do the same thing with SQLAlchemy directly ?
Here is my code :
class Referential(db.Model):
__tablename__ = "REFERENTIAL"
id = db.Column("ID", Integer, primary_key=True, autoincrement=True)
name = db.Column("NAME", String(100), index=True, unique=True, nullable=False)
internal_code = db.Column("INTERNAL_CODE", String(50), unique=True, index=True)
Thanks in advance

MSSQL's implementation when it comes to allowing nulls in a unique column is a little odd.
import sqlalchemy as sa
sa.Table(
sa.Column('column', sa.String(50), nullable=True),
sa.Index('uq_column_allows_nulls', mssql_where=sa.text('column IS NOT NULL'),
)
If you are planning on using alembic like I was this is the code:
import sqlalchemy as sa
import alembic as op
op.create_index(
name='uq_column_name',
table_name='table',
columns=['column'],
mssql_where=sa.text('column IS NOT NULL'),
)
This uses the sql expression text for sqlalchemy and create_index's dialect_expression key word arguments mssql_where=

Related

Django migrate column is not the same data type as referencing column

I have the below model which is an existing DB model and through Django's inspectdb management command the below model is created.
class ExistingLegacyModel(models.Model):
period = models.TextField(db_column="Period", blank=True, null=True)
key = models.AutoField(db_column="OutlookKey", primary_key=True)
class Meta:
managed = False
db_table = "table_name"
and currently, I'm trying to create a model with a field foreign key reference to the existing legacy DB model
class TestModel(models.Model):
period = models.ForeignKey(
ExistingLegacyModel,
on_delete=models.CASCADE,
db_column="OutlookKey",
)
so when I run the makemigrations command the migration file is successfully getting created with no issue. below is the migration file content.
class Migration(migrations.Migration):
initial = True
dependencies = [
('historical', '0011_phoenixcontractprice'),
]
operations = [
migrations.CreateModel(
name='TestModel',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('period', models.ForeignKey(db_column='OutlookKey', on_delete=django.db.models.deletion.CASCADE, to='app.ExistingLegacyModel')),
],
),
]
so now when i run the migrate command now, it is failing and giving the below error.
django.db.utils.ProgrammingError: ('42000', "[42000] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Column 'table_name.OutlookKey' is not the same data type as referencing column 'version_testmodel.OutlookKey' in foreign key 'version_testmodel_OutlookKey_eb16c31c_fk_table_name_OutlookKey'. (1778) (SQLExecDirectW); [42000] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]Could not create constraint or index. See previous errors. (1750)")
I'm stuck with this issue for the past couple of days and I searched all over the internet but didn't get any resolution. I found a couple of StackOverflow questions that are very similar to my issue, but those questions are also unanswered.
Django - Migration foreign key field type not matching current type
Django 3.2 update AutoField to BigAutoField backward compatibility with foreign key relations
I'm currently using Django 3.2.13 and mssql-django to connect to the MSSQL database.
Any help on this will be highly appreciated! Thank you in advance.
UPDATE 1
I ran the sqlmigrate command for the initial migration. So for the period column, it is creating the table with a foreign key field with big int [OutlookKey] bigint NOT NULL whereas the existing legacy model has a normal integer field.
ALTER TABLE [<app>_<model>] ADD CONSTRAINT [<app>_<model>_OutlookKey_3505d410_fk_<existing_legacy_table>_OutlookKey] FOREIGN KEY ([OutlookKey]) REFERENCES [<existing_legacy_table>] ([OutlookKey]);

Get key name in an existing provider

I want to find out what key name provided from an external provider my database is using in an encrypted database.
This is an example taken from Microsoft website.
CREATE ASYMMETRIC KEY EKM_askey1
FROM PROVIDER EKM_Provider1
WITH
ALGORITHM = RSA_2048,
CREATION_DISPOSITION = CREATE_NEW
, PROVIDER_KEY_NAME = 'key10_user1' ;
GO
But I don't know how to learn whether this is CREATE_NEW or OPEN_EXISTING and have no clue what view contains information about this key10_user1 as mentioned in the example.
Could you try:
SELECT * FROM sys.cryptographic_providers;
to get the provider id and then query using sys.dm_cryptographic_provider_keys:
SELECT * FROM sys.dm_cryptographic_provider_keys(1234567);
GO

Django SQL Server ForeignKey that allows NULL

I've been struggling with this issue for some time. I'm switching one of my apps from Postgres to SQL Server database and I'm facing an issue with ForeignKey field. I'm running latest SQL Server version with Django 1.11 and using django-pyodbc-azure app.
class Owner(models.Model):
name = models.TextField()
dog = models.ForeignKey('Dog', related_name='+')
class Dog(models.Model):
name = models.TextField()
owner = models.ForeignKey('Owner', null=True, related_name='+')
When I try to insert a new record I get the following message:
dog = Dog.objects.create(name='Rex')
owner = Owner.objects.create(name='Mike', dog=dog)
dog.owner = owner
dog.save()
('23000', u"[23000] [Microsoft][ODBC Driver 13 for SQL Server][SQL
Server]Violation of UNIQUE KEY constraint
'UQ__owner__EF6DECB9214EF1D9'. Cannot insert duplicate key in object
'dbo.owner'. The duplicate key value is (NULL). (2627)
(SQLExecDirectW)")
The label owner is the problem, try:
dog.owner_id = owner.id

How to make the 'public' schema default in a Scala Play project that uses PostgreSQL?

I am not sure if my issue connecting to the Scala Play 2.5.x Framework or to PostgreSQL so I am going to describe my setup.
I am using the Play 2.5.6 with Scala and PostgreSQL 9.5.4-2 from the BigSQL Sandboxes. I use the Play Framework default evolution package to manage the DB versions.
I created a new database in BigSQL Sandbox's PGSQL and PGSQL created a default schema called public. I use this schema for development.
I would like to create a table with the following script (1.sql in DB evolution config):
# Initialize the database
# --- !Ups
CREATE TABLE user (
id SERIAL PRIMARY KEY,
name TEXT NOT NULL,
email TEXT NOT NULL,
creation_date TIMESTAMP NOT NULL
);
# --- !Downs
DROP TABLE user;
Besides that I would like to read the table with a code like this:
val resultSet = statement.executeQuery("SELECT id, name, email FROM public.user WHERE id=" + id.toString)
I got an error if I would like to execute any of the mentioned code or even if I use the CREATE TABLE... code in pgadmin. The issue is with the user table name. If I prefix it with public (i.e. public.user) everything works fine.
My questions are:
Is it normal to prefix the table name with the schema name every time? It seems to odd to me.
How can I make the public schema a default option so I do not have to qualify the table name? (e.g. CREATE TABLE user (...); will not throw an error)
I tried the following:
I set the search_path for my user: ALTER USER my_user SET search_path to public;
I set the search_path for my database: ALTER database "my_database" SET search_path TO my_schema;
search_path correctly shows this: "$user",public
I got the following errors:
In Play: p.a.d.e.DefaultEvolutionsApi - ERROR: syntax error at or near "user"
In pgadmin:
ERROR: syntax error at or near "user"
LINE 1: CREATE TABLE user (
********** Error **********
ERROR: syntax error at or near "user"
SQL state: 42601
Character: 14
This has nothing to do with the default schema. user is a reserved word.
You need to use double quotes to be able to create such a table:
CREATE TABLE "user" (
id SERIAL PRIMARY KEY,
name TEXT NOT NULL,
email TEXT NOT NULL,
creation_date TIMESTAMP NOT NULL
);
But I strongly recommend not doing that. Find a different name that does not require a quoted identifier.

NHibernate bug sql generator

i have class named Group
i tried to test configuration:
var cfg = new Configuration();
cfg.Configure();
cfg.AddAssembly(typeof (Group).Assembly);
new SchemaExport(cfg).Execute(true, true, false);
when i debug this, im getting following sql:
create table Group (
ID INT IDENTITY NOT NULL,
Name NVARCHAR(255) null,
CreatedDate DATETIME null,
primary key (ID)
)
so like i remember, Group key reserved for Group By
so why NHibernate does not uses quotes like Group for table names?
BTW, I'm using Sql Server 2005 and NHibernate 2.1
If you are using a reserved keyword as your object name you should mark it with a DBMS specific sign. For example if you are using SQL Server you should put it in [] and use ` for MySql.
In this example you should write (assuming that you are using SQL server) :
<class name="Group" table="[Group]">

Resources