3
from sqlalchemy import Column, Integer, String, Table, delete, select
4
from sqlalchemy.dialects import mysql
5
from sqlalchemy.testing import AssertsCompiledSQL, fixtures
8
class _DeleteTestBase(object):
10
def define_tables(cls, metadata):
11
Table('mytable', metadata,
12
Column('myid', Integer),
13
Column('name', String(30)),
14
Column('description', String(50)))
15
Table('myothertable', metadata,
16
Column('otherid', Integer),
17
Column('othername', String(30)))
20
class DeleteTest(_DeleteTestBase, fixtures.TablesTest, AssertsCompiledSQL):
21
__dialect__ = 'default'
23
def test_delete(self):
24
table1 = self.tables.mytable
27
delete(table1, table1.c.myid == 7),
28
'DELETE FROM mytable WHERE mytable.myid = :myid_1')
31
table1.delete().where(table1.c.myid == 7),
32
'DELETE FROM mytable WHERE mytable.myid = :myid_1')
36
where(table1.c.myid == 7).
37
where(table1.c.name == 'somename'),
38
'DELETE FROM mytable '
39
'WHERE mytable.myid = :myid_1 '
40
'AND mytable.name = :name_1')
42
def test_prefix_with(self):
43
table1 = self.tables.mytable
45
stmt = table1.delete().\
46
prefix_with('A', 'B', dialect='mysql').\
49
self.assert_compile(stmt,
50
'DELETE C D FROM mytable')
52
self.assert_compile(stmt,
53
'DELETE A B C D FROM mytable',
54
dialect=mysql.dialect())
57
table1 = self.tables.mytable
59
talias1 = table1.alias('t1')
60
stmt = delete(talias1).where(talias1.c.myid == 7)
62
self.assert_compile(stmt,
63
'DELETE FROM mytable AS t1 WHERE t1.myid = :myid_1')
65
def test_correlated(self):
66
table1, table2 = self.tables.mytable, self.tables.myothertable
68
# test a non-correlated WHERE clause
69
s = select([table2.c.othername], table2.c.otherid == 7)
70
self.assert_compile(delete(table1, table1.c.name == s),
71
'DELETE FROM mytable '
72
'WHERE mytable.name = ('
73
'SELECT myothertable.othername '
75
'WHERE myothertable.otherid = :otherid_1'
78
# test one that is actually correlated...
79
s = select([table2.c.othername], table2.c.otherid == table1.c.myid)
80
self.assert_compile(table1.delete(table1.c.name == s),
81
'DELETE FROM mytable '
82
'WHERE mytable.name = ('
83
'SELECT myothertable.othername '
85
'WHERE myothertable.otherid = mytable.myid'