2012-09-17 23 views
7

SQLAlchemy और Django models.py में लिखी गई इस सरल तालिका को देखते हुए, मैं यूपीसी को अनूठा होने के लिए कैसे सेट करूं? यूपीसी सभी वस्तुओं के लिए उपलब्ध नहीं होगा, लेकिन यदि यह अद्वितीय होना चाहिए।अद्वितीय नहीं है अगर शून्य 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

शून्य मूल्यों के साथ एकाधिक पंक्तियों अद्वितीय बाधा के लिए एक समस्या नहीं होनी चाहिए। केवल "मान" अद्वितीय होना चाहिए, न्यूल कोई मूल्य नहीं है।

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

ठीक है, कि समझ में आता है आप की कोशिश की है ?:। मुझे एहसास हुआ कि मैंने उस एप्लिकेशन में कहीं गलती की हो सकती है जहां मैं शून्य करना चाहता हूं, लेकिन मैं वास्तव में एक खाली स्ट्रिंग डाल सकता हूं जिससे विशिष्टताएं समस्याएं पैदा होंगी। – MCH