मुझे एक डिलीट को कैस्केड करने में समस्या है। मैं दो तालिकाओं है, और वे कई-से-अनेक मैप की जाती हैं:कई से कई एसोसिएशन टेबल में एक डिलीट कैस्केडिंग?
class File(object): pass
file_table = Table('file', metadata,
Column('id', Integer, primary_key=True, autoincrement=True),
Column('filename', String(255)),
}
class FileHost(object): pass
file_host = Table('host', metadata,
Column('id', Integer, primary_key=True, autoincrement=True),
Column('name', String(255)),
)
file_hosted = Table('file_hosted', metadata,
Column('id_host', Integer, ForeignKey('host.id')),
Column('id_file', Integer, ForeignKey('file.id'))
)
session.mapper(File, file_table, properties={
'host': relation(FileHost, secondary=file_hosted, backref='files',
cascade='all,delete-orphan', single_parent=True)
})
session.mapper(FileHost, file_host)
यह है त्रुटि मैं:
sqlalchemy.exc.IntegrityError:
(IntegrityError) update or delete on table "file" violates
foreign key constraint "file_hosted_id_file_fkey" on table "file_hosted"
DETAIL: Key (id)=(50905) is still referenced from table "file_hosted".
किसी को भी एक विचार मैं गलत क्या कर रहा मिला है?
मैं भी सवाल on the sqlalchemy mailing list कहा, और सही जवाब मिल गया:
आप SQLAlchemy फ़ाइल FileHost को हटाता है झरना कह रहे हैं, लेकिन आप यह दूसरी तरह के आसपास चाहते हैं। आप कैस्केड = 'सब, डिलीट-अनाथ' और सिंगल_पैरेंट = बैकफ़्रफ़ में सही खंडों को स्थानांतरित करके इसे ठीक कर सकते हैं। आप शायद uselist = झूठी भी चाहते हैं।
session.mapper(File, file_table, properties={
'host': relation(FileHost,
backref=backref('files',
cascade='all,delete-orphan',
single_parent=True),
secondary=file_hosted,
uselist=False)
})
आप वास्तव में कैस्केड करना चाहते हैं: 'फाइलहोस्ट' को हटाने के लिए 'फ़ाइल' को हटाने, 'फाइलहोस्ट' को हटाने के लिए 'फाइल' या दोनों को हटाने के लिए? – van
जब मैं 'फ़ाइल' हटाता हूं तो इसे 'file_hosted' में उचित पंक्तियों को हटा देना चाहिए। – tom
क्या आप एक कोड स्निपेट जोड़ सकते हैं जो इस त्रुटि को उत्पन्न करता है? – van