I'm building a webpage which uses SQL Server as the database + Flask for the web framework. I want to be able to add users to my webpage, and created a SQLAlchemy class as below:
from datetime import datetime
from app import db
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
email = db.Column(db.String(), unique=True)
password = db.Column(db.String())
created_at = db.Column(db.DateTime(), default=datetime.now())
def __repr__(self):
return self.username
However, when I try to create the tables SQL Server gives me the following error:
sqlalchemy.exc.ProgrammingError: (pyodbc.ProgrammingError) ('42000', "[42000] [Microsoft][ODBC SQL Server Driver][SQL Server]Column 'email' in table 'user' is of a type that is invalid for use as a key column in an index. (1919) (SQLExecDirectW); [42000] [Microsoft][ODBC SQL Server Driver][SQL Server]Could not create constraint or index. See previous erro
rs. (1750)")
[SQL:
CREATE TABLE [user] (
id INTEGER NOT NULL IDENTITY(1,1),
email VARCHAR(max) NULL,
password VARCHAR(max) NULL,
created_at DATETIME NULL,
PRIMARY KEY (id),
UNIQUE (email)
)]
If I remove the Unique=True
it works fine. I guess it has something to do with the SQL Server not understanding whats going on in the class or vice-versa.
Any ideas how to make a key Unique in SQL Server from SQLAlchemy?