<"29676004 :flex;nav__ght:363 menu"><16 href="/-4766-menu ><1"u">
<7296338883 href="/-4766__box3 href="/-4766__box--h{de3 href="/-4766__box--:flex3904456103 ca="jsx-504766363 menu825932626" 0 40 40">A SQLAlchemy C:#9t Sheesu-h1>mvelopersMar .3, 2015">Feb 10, 2017">/cache=a="iry:max3atmenu=git-de20/K8JoHnzCTgyeu2ms5pZftop:ateY(0);-msht-itemdd"ls-developelazyload-placeholde4441104051104051104051velopers#introduiv,.ad aria-h{disp9 9 ue"l i 4766363faefa-="40" aria-h{disp9 9 ue"l /is="a> Introduiv,.au-h2>

SQLAlchemy is="2deep d-lipo e4f #basic <4831developela103 ca-ategor">from sqlalchemy.:cendec catjsx- import dec catjsx-_gory from sqlalchemy.orm import scel d_sdssion,sdssionmaker from zel .sqlalchemy import Zel T 0 0aiv,.aEx103 ,.a from sqlalchemy import ( Column, Integ><, bs" ng, Boolean, Fjot-inKey, DatlTime, bequsx-3, Float ) import datltime DBSdssion = scel d_sdssion(sdssionmaker(ex103 ,.a=Zel T 0 0aiv,.aEx103 ,.a())) Bory = dec catjsx-_gory() evelo Book(Bory): #<------------------------- ;posile0445__ = "books" #matches the 0445 of the aivual category tegle id = Column(Integ><,bequsx-3('book_sdq'),primary_key=T ue) #tplays ni-39-ite ala majjobsategory e45610s 0445 = Column(bs" ng(50)) #9.86log column neld le0gths rcustolid = Column(Integ><,Fjot-inKey('rcustos.id')) # eloumes thereeis="2tegle in the sategory calald 'rcustos' th9t has="n 'id' column pric5 = Column(Float) datl_adisd = Column(DatlTime, defaul 3datltime.datltime.now) # defaul gron bd specifisd as=funiv,.as promote = Column(Boolean,defaul 3Falry) #99999to rspaalues #ory"iesd" d-

<.862ng>Scateglog d-liFilt3 categ.862ng>x-3> <4831>#fetch -lerytyle: lBooks = DBSdssion.ory">(Book) #returns="2lass= objdiv. "joboBook in lBooks: printboBook.0445 #simple86.4t3 s lBooks = DBSdssion.ory">(Book).6.4t3 _by(rcustolid=1) #returns="ll the books="joba specific rcusto #mjotlassplex86.4t3 s lBooks = DBSdssion.ory">(Book).6.4t3 (Book.pric5<20) #returns="ll the books=-ite pric5 <20. Note we=usl86.4t3 ,9not 6.4t3 _by #6.4t3 sgron bd assb610d lBooks = DBSdssion.ory">(Book).6.4t3 _by(rcustolid=1).6.4t3 (Book.pric5<20) #"ll books=byba specific rcusto,=-ite pric5<20 #4or:cal o56103 catgron bd usld in 6.4t3 s from sqlalchemy import tol lBooks = DBSdssion.ory">(Book).6.4t3 (tol(Book.pric5<20,promote==T ue)) #treturns="ll books= th9t cass 4ess thon 20 OR areebe03 promoted #ord3 cat from sqlalchemy import "/jo DBSdssion.ory">(Book).ord3 _by(Book.pric5) #get "ll books=ord3 ld bybpric5 DBSdssion.ory">(Book).ord3 _by("/jo(Book.pric5)) #get "ll books=ord3 ld bybpric5 "/joen_log #other uslf(Book).count() #returns=the 0umber of books DBSdssion.ory">(Book).offset(5) #offset=the atmul byb5 DBSdssion.ory">(Book).limit(5) #treturn 9t mass 5 books DBSdssion.ory">(Book).fi;st() #return=the fi;st book only9to None DBSdssion.ory">(Book).get(8) #return=the Book -ite primary key = 3, to None #rcommuonships" aria-h{disp9 9 ue"l i 4766363faefa-="40" aria-h{disp9 9 ue"l /is="a> Rcommuonshipsu-h2>

Rcommuonships l(/asse SQL tegles aree"/jobsbdd in t3 ms of fjot-in key rcommuonships. From the sxample, the <4831>booksevelo Book(Bory): ;posile0445__ = "books" #matches the 0445 of the aivual category tegle id = Column(Integ><,bequsx-3('book_sdq'),primary_key=T ue) 0445 = Column(bs" ng(50)) rcustolid = Column(Integ><,Fjot-inKey('rcustos.id')) rcusto = rcommuonship("Acusto",baik<,bequsx-3('book_sdq'),primary_key=T ue) 0445 = Column(bs" ng(50)) "Acusto"books

Hereearee" few -ays you ron make=usl8of the rcommuonship ox-39itgls=configured:x-3> <4831developela103 ca-ategor">oBook = DBSdssion.ory">(Book).6.4t3 _by(0445i"Harry Pott3 d-lithe /eegods=ofprmmuonal-39").fi;st() oAcusto = oBook.acusto2 # oAcusto ls=nowiaa Acusto lnstanc . oAcusto.id == oBook.acustolid #itgworks the other -ay as=wela oAcusto = DBSdssion.ory">(Acusto).6.4t3 _by(0445i"Orsaa Scott Card") "joboBook in oAcusto.books: printboBook.0445 #adilog d9new book oNh fiok = Book() oBook.0445 = "En99;'s G445" oBook.acusto2= oAcusto #adilog d9new book in ac.iflersxt -ay... oNh fiok = Book() oBook.0445 = "En99;'s Shadow" oAcusto.books.m:trnd(oBook) One to one rcommuonshipsu-.862ng>x-3> <4831developela103 ca-ategor">evelo Parsxt(Bory): ;posile0445__ = 'parsxt' id = ColumnColumn(Integ><,bequsx-3('p_sdq'),primary_key=T ue) child_id = Column(Integ><, Fjot-inKey('child.id')) child = rcommuonship("Child", baik<,bequsx-3('c_sdq'),primary_key=T ue) <. If the .86log <4831>"parsxt" <4831developela103 ca-ategor">oChild = DBSdssion.ory">(Child).get(1) oParsxt2= oChild.parsxt oParsxt22= Parsxt() oParsxt.child = Child() M9ny to m9ny rcommuonshipsu-.862ng>x-3>

A m9ny to m9ny rcommuonship rcquires="n exu a2tegle to create mapp03 s l(/asse ls" s. Thereeareetwo -ays of do03 usis:x-3>

Fi;st,pjusteuslog /stels:x-3> <4831developela103 ca-ategor">evelo Ci>(Bory): ;posile0445__ = 'ci><,bequsx-3('cat_sdq'),primary_key=T ue) 0445 = Column(bs" ng(20)) evelo Produiv(Bory): ;posile0445__ = 'produivs' id = Column(Integ><,bequsx-3('prod_sdq'),primary_key=T ue) 0445 = Column(bs" ng(20)) evelo Map(Bory): ;posile0445__ = 'map' id = Column(Integ><,bequsx-3('map_sdq'),primary_key=T ue) cat_id = Column(Integ><,Fjot-inKey('ci><,Fjot-inKey('produivs.id')) MapMapMap

This=nexu=approach ls=l(/t3 if your map2tegle is only9a map2tegle d-lirequires=nolassplex8

<4831developela103 ca-ategor">map_tegle = Tegle('maps', Base./eeacate, Column('cat_id', Integ><, Fjot-inKey('ci><, Fjot-inKey('produivs.id')) ) evelo Ci>(Bory): ;posile0445__ = 'ci><,bequsx-3('cat_sdq'),primary_key=T ue) 0445 = Column(bs" ng(20)) produivs = rcommuonship("Produiv", secandary=map_tegle,2 # you ron alro usl8the .86log 0445 of the tegle,2"maps", as the secandary baik<,bequsx-3('prod_sdq'),primary_key=T ue) 0445 = Column(bs" ng(20)) <4831developela103 ca-ategor">#can.86uiv a ei> d-liadd some produivs to = Ci>() oCi>.0445 = "Books" oProduiv2= Produiv() oProduiv.0445 = "En99;'s G445 - Orsaa Scott Card" oCi>.produivs.m:trnd(oProduiv) oProduiv2= Produiv() oProduiv.0445 = "Harry Pott3 d-lithe /eegods=ofpRmmuonal-39" oProduiv.ci> "joboProduiv2in oCi>.produivs: printboProduiv.0445 #
(Produiv).6.4t3 _by(0445i"") "joboCi> in oProduiv.ci>.0445 Self x-3>

Sometimes you hax- a2tegle -ite a fjot-in key point03 9t the s445 tegle. Fjobsxample, s4y we=hax- a2bunch ofpcates in ac.irdivld tre . A n831deon hax- m9ny child cates but 9t mass one parsxtx-3> <4831developela103 ca-ategor">evelo Tre ss="(Bory): ;posile0445__ = 'cates' id = Column(Integ><,bequsx-3('cate_sdq'),primary_key=T ue) parsxt_id = Column(Integ><,Fjot-inKey('cates.id')) 0445 = Column(bs" ng(20)) childrsx = rcommuonship("Tre ss="", baik <4831developela103 ca-ategor">#fetch the root n831d(eloume thereeis=one n831d-ite no parsxts) oRootss="2= DBSdssion.ory">(Tre ss=").6.4t3 _by(parsxt_id=None).fi;st() #

Multiple8rcommuonships -ite the s445 tegleu-.862ng>x-3> <4831developela103 ca-ategor">evelo Wikilass(Bory): ;posile0445__ = 'psts{' id = Column(Integ><,bequsx-3('psts_sdq'),primary_key=T ue) 0445 = Column(bs" ng(20)) rcustolid = Column(Integ><,Fjot-inKey('uslrs.id')) edittolid = Column(Integ><,Fjot-inKey('uslrs.id')) editto = rcommuonship("Uslr", primary2.40 = "Wikilass.edittolid == Uslr.id",baik<,bequsx-3('usr_sdq'),primary_key=T ue) 0445 = Column(bs" ng(20)) <4831developela103 ca-ategor">oAcusto = DBSdssion.ory">(Uslr).6.4t3 _by(0445i"Sheena O'Connell") #aa .custo ext-ds a osts oPass = Wikilass() oPass.0445 = "Sqlalchemy C:#9t Shees" oPass.rcusto2= oAcusto # 1 0r .a another uslr edits (Uslr).6.4t3 _by(0445i"Yi-Jirr C:#n") oEditto.edited_psts{.m:trnd(oPsts) #
#e45610-canfigurav,.ad aria-h{disp9 9 ue"l i 4766363faefa-="40" aria-h{disp9 9 ue"l /is="a> E45610 Configurav,.au-h2>

<.862ng>Conneiv,.a bs" ngsu-.862ng>x-3> <4831developela103 ca-ategor">#the genv al fjom=ofp" canneiv,.a .86log: `dpereiv+driver://uslr0445:pelo6103@hass:port/category` #SQLITE: 'sql-3e:///:memory:' #stjotl-lerytyle: in memory, cateeis=lass when p="jsx- exits 'sql-3e:////absolute-3081/to0p="jdiv.db') #Unix/Mac 'sql-3e:///C:\\3081\\to\\3="jdiv.db' #Windows r'sql-3e:///C:\3081\to\3="jdiv.db' #Windows al lr04jsx- #lass="jSQL '03 categql://uslr:pelo@localhass/mycategory' '03 categql+psycopg2://uslr:pelo@localhass/mycategory' '03 categql+pginde://uslr:pelo@localhass/mycategory' #O aile 'o aile://uslr:pelo@127.0.0.1:1521/sid0445' 'o aile+cx_o aile://uslr:pelo@tns0445' #Microsoft SQL Serler 'msgql+pyodbc://uslr:pelo@mycsn' 'msgql+pymsgql://uslr:pelo@hass0445:port/cb0445' E45610, Sdssion d-liBcateg.862ng>x-3> <4831developela103 ca-ategor">#set=up the s45610 s45610 = create_s45610(sConneiv,.abs" ng, ehh.=T ue) #ehh.=T ue makes the sqllassm9nds=issuld bybsqlalchemy get output to the can.ole, uslf #canclus,.ad aria-h{disp9 9 ue"l i 4766363faefa-="40" aria-h{disp9 9 ue"l /is="a> Canclus,.au-h2>

Thereeareem9ny mjotl-ays in which you ron canfigure rcommuonships,em9ny mjotl-ays to ory"> ac.ategory, d-lim9ny topict th9t thls=c:#9t lheet justedid0't caler. SQLAlchemy is pretty9hugy, but "jobm9ny m:tlic03 catgyou don't neld to do 9nytyle: mjotlassplex8thon wh9t is shown here. If you ro45 hereelook03 to 44561 how to ury SQLAlchemy d-liyou feel you areein acpnced" i whereeyou ron make=usl8of the /eegods=Ie"/jobsbdd hereethen Igwould sugg0steds d9next liepeyou rea_=up aelittle8bitgo1 how to ury the session to m9n0% t 0 0aiv,.as.x-3> <104051104051104051104051velopersSQL04456cl476636314px-ou-39004ns="httpunity_blu/topic3sqlalchemy">Sqlalchemyu-ite104051velopers

E4joy thls=03 c? Gsx- <.862ng>Sheenacl.862ng> aeliky if it's helpf110405181040510 mes450% r__aiv,.as"em81040510 mergin-bottsNot 14px:#9rt"l i 4766363faefa-:#9rt-o" aria-h{disp9 9 ue"l /ismvelopers91104051104051cl476636314pxfsx-e-only9s450% r__unitjsx-g>#caitjsxs-du107lawl441ss="jsx-183080000l-3e-cdn94831 ">
i 4766363faefa-="roepsquroepo" aria-h{disp9 9 ue"l /ism
<"410704710 bontom-ba;363 menu">Sheenacl.862ng>1104051velopers<"410704710 b4pxprimary">get lirgied1104051104051104051104051velopers1velopersLeax- a2like=9nd caitjsx "job<.862ng>Sheenacl.862ng>x-3>110405110405181040510 mes450% r__aiv,.as"em81040510 mergin-bottsNot 14px:#9rt"l i 4766363faefa-:#9rt-o" aria-h{disp9 9 ue"l /ismvelopers91104051104051cl476636314pxfsx-e-only9s450% r__unitjsx-g>#caitjsxs-du107lawl441ss="jsx-183080000l-3e-cdn94831 ">
tabsg>(Bory):\n ;posile0445__ = 'ci><,bequsx-3('cat_sdq'),primary_key=T ue) \n 0445 = Column(bs" ng(20))\n\nevelo Produiv(Bory):\n ;posile0445__ = 'produivs'\n id = Column(Integ><,bequsx-3('prod_sdq'),primary_key=T ue) \n 0445 = Column(bs" ng(20))\n \nevelo Map(Bory):\n ;posile0445__ = 'map'\n id = Column(Integ><,bequsx-3('map_sdq'),primary_key=T ue) \n cat_id = Column(Integ><,Fjot-inKey('ci><,Fjot-inKey('produivs.id'))\n```\n\nHereeyou ron specify rcommuonships .a the `Map`2evelo. The benefitgof thls=approach ls=th9t you ron lnstantiate the `Map`2evelo, thls=is=uslf<, Fjot-inKey('ci><, Fjot-inKey('produivs.id'))\n)\n\nevelo Ci>(Bory):\n ;posile0445__ = 'ci><,bequsx-3('cat_sdq'),primary_key=T ue) \n 0445 = Column(bs" ng(20))\n\n produivs = rcommuonship(\"Produiv\",\n secandary=map_tegle,2 # you ron alro usl8the .86log 0445 of the tegle,2\"maps\", as the secandary\n baik<,bequsx-3('prod_sdq'),primary_key=T ue) \n 0445 = Column(bs" ng(20))\n\n```\nYou ron make=usl8of the rcommuonship like=usis:\n\n```ategor\n#can.86uiv a ei> d-liadd some produivs to = Ci>()\noCi>.0445 = \"Books\"\n\noProduiv2= Produiv()\noProduiv.0445 = \"En99;'s G445 - Orsaa Scott Card\"\noCi>.produivs.m:trnd(oProduiv)\n\noProduiv2= Produiv()\noProduiv.0445 = \"Harry Pott3 d-lithe /eegods=ofpRmmuonal-39\"\noProduiv.ci>\nfjoboProduiv2in oCi>.produivs:\n printboProduiv.0445\n \n#
(Produiv).6.4t3 _by(0445i\"\")\nfjoboCi> in oProduiv.ci>.0445\n \n```\n\n**Self (Tre ss=").6.4t3 _by(parsxt_id=None).fi;st()\n\n#idiv aiv -ite childrsx of exitslog n831\nfjoboChild in oRootss=".childrsx:\n printboChild.0445\n\n#create new rcommuonships\n\noParsxt = Tre ss="()\noParsxt.0445 = \"parsxt\"\noRootss=".childrsx.m:trnd(oParsxt)\r\noChild = Tre ss="()\noChild.0445 = \"Child\"\noChild.parsxt2= oParsxt\n```\n\n**Multiple8rcommuonships -ite the s445 tegle**\n \n```ategor\nevelo Wikilass(Bory):\n ;posile0445__ = 'psts{'\n id = Column(Integ><,bequsx-3('psts_sdq'),primary_key=T ue) \n 0445 = Column(bs" ng(20))\n rcustolid = Column(Integ><,Fjot-inKey('uslrs.id'))\n edittolid = Column(Integ><,Fjot-inKey('uslrs.id'))\n\n editto = rcommuonship(\"Uslr\", primary2.40 = \"Wikilass.edittolid == Uslr.id\",baik<,bequsx-3('usr_sdq'),primary_key=T ue) \n 0445 = Column(bs" ng(20))\n```\nYou ron
(Uslr).6.4t3 _by(0445i\"Sheena O'Connell\")\n\n#aa .custo ext-ds a osts\noPass = Wikilass()\noPass.0445 = \"Sqlalchemy C:#9t Shees\"\noPass.rcusto2= oAcusto\n\n#41 0r .a another uslr edits (Uslr).6.4t3 _by(0445i\"Yi-Jirr C:#n\")\noEditto.edited_psts{.m:trnd(oPsts)\n\n#idiv aiv -ite exitslog rcommuonships\nfjoboPass in oAcusto.rcustoed_psts{:\n printboPass.0445\n \n"joboPass in oEditto.edited_psts{:\n printboPass.0445\n```\n\n## E45610 Configurav,.a\n\n**Conneiv,.a bs" ngs**\n```ategor\n#the genv al fjom=ofp" canneiv,.a .86log:\n`dpereiv+driver://uslr0445:pelo6103@hass:port/category` \n\n#SQLITE:\n'sql-3e:///:memory:' #stjotl-lerytyle: in memory, cateeis=lass when p="jsx- exits\n'sql-3e:////absolute-3081/to0p="jdiv.db') #Unix/Mac\n'sql-3e:///C:\\\\3081\\\\to\\\\3="jdiv.db' #Windows\nr'sql-3e:///C:\\3081\\to\\3="jdiv.db' #Windows al lr04jsx-\n\n#lass="jSQL\n\n'03 categql://uslr:pelo@localhass/mycategory'\n'03 categql+psycopg2://uslr:pelo@localhass/mycategory'\n'03 categql+pginde://uslr:pelo@localhass/mycategory'\n\n#O aile\n'o aile://uslr:pelo@127.0.0.1:1521/sid0445'\n'o aile+cx_o aile://uslr:pelo@tns0445'\n\n#Microsoft SQL Serler\n'msgql+pyodbc://uslr:pelo@mycsn'\n'msgql+pymsgql://uslr:pelo@hass0445:port/cb0445'\n```\n\n**E45610, Sdssion d-liBcat**\n\n```ategor\n#set=up the s45610\ns45610 = create_s45610(sConneiv,.abs" ng, ehh.=T ue) #ehh.=T ue makes the sqllassm9nds=issuld bybsqlalchemy get output to the can.ole, uslf ac.ategory, d-lim9ny topict th9t thls=c:#9t lheet justedid0't caler. SQLAlchemy is pretty9hugy, but "jobm9ny m:tlic03 catgyou don't neld to do 9nytyle: mjotlassplex8thon wh9t is shown here. If you ro45 hereelook03 to 44561 how to ury SQLAlchemy d-liyou feel you areein acpnced" i whereeyou ron make=usl8of the /eegods=Ie"/jobsbdd hereethen Igwould sugg0steds d9next liepeyou rea_=up aelittle8bitgo1 how to ury the session to m9n0% t 0 0aiv,.as. ","createdAt":1426066161,"ci>5ge:urStatus":{},"s456chRtmul Agin-bos":[],"pagloat,.aMapa:{},"s456chIn"jMapa:{},"isLoad03 Mjot":falry,"topWxt-drta:{"acustos":[]},"isS456ch03 ":falry,"noMjotFeedAgin-bos":falry,"isLoad03 ":falry,"currsxtCi>a:{},"sugg0stedCi>Sugg0st,.asa:[],"isSav03 ":falry,"isLoad03 ":falry,"isPub ith03 ":falry,"seenGrelslogd:9 ue,"lassSavedAt":null},"jobs":{"sugg0stedJob":{}},"rcusd:{"isSend03 ":falry,"isOtrn":falry,"errtoMessegs :"","setslogta:{"acusTyp56:"SOCIAL_SIGNIN",ercusTarget6:"CM","t aiklogEvjsxPrefix-:"",ere.irdivToWhenLoglo":"",eaf-drCanfirmUrl4:1"}}}},"308104456:"/rgin-bo/showm,"ory">4:{"uslr0445":"sheena",eslug":"un99;stan_log-sqlalchemy-c:#9t-lheet-du107lawl4},"buildId":"abf9f69c-8402-4af3-a501-c17b91e19259","buildStatta:{"app.js":{"hassd:"7ac45fe04bfdfind9bd66dd7fb93ac83"}},"eloexPrefix-:"83080000l-3e-cdn94831 "><3 cat5103 cat"async=""ons="__NEXT_PAGE__/rgin-bo/showm typ0="tor:/java03 cat""jsx-183080000l-3e-cdn94831 "><3 cat5103 cat"async=""ons="__NEXT_PAGE__/_errto4 typ0="tor:/java03 cat""jsx-183080000l-3e-cdn94831 "><3 cat510405110405103 cat"typ0="tor:/java03 cat""jsx-183080000l-3e-cdn94831 "><3 cat51104051="40 rco="(0);-lheet"g>83080000fe-csegoogboaplsnu">/css?family=Otrn+S0 0:300,400,600,700top:="40 rco="(0);-lheet"g>83080000otrn