Unique if not null SQLAlchemy and Django

Given this simple table, written in SQLAlchemy and Django models.py, how would you establish that the UPC will be unique, if not null. UPC will not be available for all elements, but if it should be unique.

class Products(base): __tablename__ = u'products' id = Column(Integer(), primary_key=True, autoincrement = True) product_name = Column(String(), unique=True, nullable=False) upc = Column(String(), nullable = True) 

and

 class Products(models.Model): id = models.AutoField(primary_key=True) product_name = models.TextField() upc = models.TextField(null=True, blank=True) 
+7
source share
1 answer

Multiple lines with NULL values ​​should not be a problem for a unique constraint. Only "values" must be unique; NULL does not matter.

You tried?:

 upc = Column(String(), unique=True, nullable=True) 
+7
source

All Articles