2012-09-17 3 views
7

SQLAlchemy 및 Django models.py로 작성된이 간단한 테이블을 감안할 때 null이 아닌 경우 UPC를 고유하게 설정하는 방법은 무엇입니까? UPC는 모든 항목에 대해 사용할 수 없지만 고유해야합니다. NULL 값이null이 아닌 고유하지 않은 SQLAlchemy 및 Django

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) 

그리고

class Products(models.Model): 
    id = models.AutoField(primary_key=True) 
    product_name = models.TextField() 
    upc = models.TextField(null=True, blank=True) 

답변

7

여러 행이 고유 제한 조건에 대한 문제가되지 않습니다. "값"만 고유해야하며 NULL은 값이 없습니다.

는 시도 있나요?

upc = Column(String(), unique=True, nullable=True) 
+2

좋아, 그 의미가 있습니다. 내가 null을 원할 때 응용 프로그램에서 어딘가에서 실수를했을 수도 있다는 것을 깨달았지만 실제로는 고유 한 문제를 일으킬 수 있도록 빈 문자열을 넣을 수도 있습니다. – MCH

관련 문제