Note that there are some explanatory texts on larger screens.

plurals
  1. POUpdating three way junction table in SqlAlchemy
    primarykey
    data
    text
    <p><strong>Update</strong></p> <p>The problem is that when the object is persited into the database for every change a new <strong>INSERT</strong> expession is generated. What I need is only one <strong>INSERT</strong> which to include all of the columns.</p> <pre class="lang-python prettyprint-override"><code>#First **INSERT** request plane.hangar.appen(hangar) #Second **INSERT** request users.planes.append(plane) </code></pre> <p>I have the following database structure described with <strong>SqlAlchemy</strong>. What I want to do is to insert a new record in the table "<strong><code>users_planes_hangars</code></strong>". I'm trying to do that in the following way:</p> <pre class="lang-python prettyprint-override"><code>plane= dbSession.query(Plane).filter(Plane.plane_id == planeId).first() garage = dbSession.query(Hangar).filter(Hangar.hangar_id == hangarId).first() plane.hangar.appen(hangar) users.planes.append(plane) dbSession.commit() </code></pre> <p>The problem is that the executed query (the query below) does not include the <code>user_id</code>, it only includes the <code>plane_id</code> and the <code>hangar_id</code>.</p> <pre class="lang-sql prettyprint-override"><code>INSERT INTO users_planes_hangars (plane_id, hangar_id) VALUES (%(plane_id)s, %(hangar_id)s) RETURNING users_planes_hangars.user_id' {'hangar_id': 4, 'plane_id': 16} </code></pre> <pre class="lang-python prettyprint-override"><code>|usersTabe| &lt;--- |users_planes_hangars_table| ---&gt; |planesTable| | \|/ |hangarsTable| class User(object): pass class Plane(object): pass class Hangar(object): pass usersMeta = Table("user", Column("user_id", Integer, primary_key = True) ) mapper(User, usersMeta, properties = { "planes": relationship(Plane, usersPlanesHangars) }) planesMeta = Table("planes", Column("plane_id", Integer, primary_key = True) ) mapper(Plane, planesMeta, properties = { "hangar": relationship(Hangar, usersPlanesHangars) }) hangarMeta = Table("hangars", Column("hangar_id", Integer, primary_key = True) ) usersPlanesHangars = Table("users_planes_hangars", Column("user_id", Integer, ForeinKey("users.user_id"), primary_key = True) Column("plane_id", Integer, ForeinKey("planes.plane_id"), primary_key = True) Column("hangar_id", Integer, ForeinKey("hangar.hangar_id"), primary_key = True) ) </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload