Mysql
 sql >> Database >  >> RDS >> Mysql

SQLAlchemy:crea query di eliminazione utilizzando l'auto-unione su MySQL

SQLAlchemy attualmente supporta UPDATE..FROM su Postgresql, MySQL e altri, ma non abbiamo ancora provato a supportare DELETE..JOIN.

Tuttavia, sembra funzionare (quasi?), Per quanto riguarda la generazione della stringa SQL:

class Path(Base):
    __tablename__ = "path"

    id = Column(Integer, primary_key=True)
    descendant = Column(Integer)
    ancestor = Column(Integer)

j = join(Path, p1, p1.ancestor == 5)
d = delete(j).where(Path.descendant == p1.descendant)
print d

stampe:

DELETE FROM path JOIN path AS p1 ON p1.ancestor = :ancestor_1 
 WHERE path.descendant = p1.descendant

Tuttavia, il mio database MySQL non lo accetta, per impostazione predefinita esegue il rendering INNER JOIN, che non riesce, ma se modifico il compilatore MySQL per non farlo, fallisce ancora:

s.execute(d)

(ProgrammingError) (1064, "You have an error in your SQL syntax; check the manual that 
corresponds to your MySQL server version for the
right syntax to use near 'JOIN path AS p1 ON p1.ancestor = 5 WHERE
path.descendant = p1.descendant' at line 1") 'DELETE FROM path JOIN
path AS p1 ON p1.ancestor = %s WHERE path.descendant = p1.descendant'
(5,)

assomiglia al tuo SQL testualmente (oh, fatta eccezione per 'cancella percorsi DA percorsi'? è giusto?) ?

In ogni caso, se il compilatore integrato non lo sta facendo, le tue opzioni sono usare session.execute("some sql") o per creare un costrutto personalizzato con l'estensione del compilatore .