def create_data(schema, cursor): film_table = schema.get_table_by_name('film') planet_table = schema.get_table_by_name('planet') char_table = schema.get_table_by_name('character') insert_ep4 = Q(film_table).insert(title='A New Hope') Q.common( Q(planet_table).insert( name='Tatooine', film=insert_ep4 ), Q(char_table).insert( name='Luke Skywalker', film=insert_ep4 ) ).execute(cursor=cursor)
def test_insert_strategy_with_explicit_reverse(immutabledb): b_table = create_table('b_table') a_table = create_table('a_table', related_table=b_table) b_query = Q(b_table).insert(col1=1) a_queries = Q.common( Q(a_table).insert(col1=2, col3=b_query), Q(a_table).insert(col2=3, col3=b_query)) sql = a_queries.to_sql() assert sql == ( b'WITH "c0" AS (INSERT INTO "b_table" ("col1") VALUES (1) RETURNING' b' "id"), "c1" AS (INSERT INTO "a_table" ("col1", "col3") VALUES (2,' b' (SELECT "c0"."id" AS "id" FROM "c0")) RETURNING "id"), "c2" AS' b' (INSERT INTO "a_table" ("col2", "col3") VALUES (3, (SELECT' b' "c0"."id" AS "id" FROM "c0")) RETURNING "id") SELECT' b' row_to_json(__tl) FROM (SELECT * FROM "c2") AS __tl')
def test_combined_queries(): a_table = create_table() (Q.common((Q(a_table).select('col1')), (Q(a_table).insert(col1=1, col2=2).select('col2'))))