<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.mediagoblin.org/index.php?action=history&amp;feed=atom&amp;title=SQLAlchemy_Tips</id>
	<title>SQLAlchemy Tips - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.mediagoblin.org/index.php?action=history&amp;feed=atom&amp;title=SQLAlchemy_Tips"/>
	<link rel="alternate" type="text/html" href="https://wiki.mediagoblin.org/index.php?title=SQLAlchemy_Tips&amp;action=history"/>
	<updated>2026-06-07T05:50:05Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.39.17</generator>
	<entry>
		<id>https://wiki.mediagoblin.org/index.php?title=SQLAlchemy_Tips&amp;diff=1471&amp;oldid=prev</id>
		<title>Unhammer: sql</title>
		<link rel="alternate" type="text/html" href="https://wiki.mediagoblin.org/index.php?title=SQLAlchemy_Tips&amp;diff=1471&amp;oldid=prev"/>
		<updated>2014-01-24T14:58:26Z</updated>

		<summary type="html">&lt;p&gt;sql&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;Some tips for hacking around with the MediaGoblin databases and SQLAlchemy.&lt;br /&gt;
&lt;br /&gt;
First off, read all of http://docs.sqlalchemy.org/en/latest/orm/tutorial.html – great tutorial. &lt;br /&gt;
&lt;br /&gt;
Then, to use MediaGoblin database classes/tables from your python interpreter, you can start up &amp;lt;tt&amp;gt;bin/python2 -i&amp;lt;/tt&amp;gt; (if you use Emacs, you can hand it &amp;lt;tt&amp;gt;/path/to/mediagoblin/bin/python2 -i&amp;lt;/tt&amp;gt; when it asks which python to start) and begin with this boilerplate:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
from sqlalchemy import create_engine&lt;br /&gt;
engine = create_engine(&amp;#039;sqlite:///:memory:&amp;#039;, echo=True)&lt;br /&gt;
# Use an in-memory table, and have all SQL statements echoed back in&lt;br /&gt;
# the interpreter for debuggability&lt;br /&gt;
&lt;br /&gt;
from mediagoblin.db.base import Session&lt;br /&gt;
Session.configure(bind=engine)&lt;br /&gt;
session=Session()&lt;br /&gt;
&lt;br /&gt;
# Some of the tables need to have some entries in order for the rest&lt;br /&gt;
# to be usable:&lt;br /&gt;
from mediagoblin.db.models import FOUNDATIONS as MAIN_FOUNDATIONS&lt;br /&gt;
for Model,rows in MAIN_FOUNDATIONS.items():&lt;br /&gt;
    for parameters in rows:&lt;br /&gt;
        new_row = Model(**parameters)&lt;br /&gt;
	session.add(new_row)&lt;br /&gt;
&lt;br /&gt;
from mediagoblin.db.base import Base&lt;br /&gt;
from mediagoblin.db.models import * # User, MediaEntry, etc&lt;br /&gt;
Base.metadata.create_all(engine) # creates all tables&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Now try it out:&lt;br /&gt;
&amp;lt;pre&amp;gt;&lt;br /&gt;
session.query(User).all()       # An empty list&lt;br /&gt;
&lt;br /&gt;
goblinda=User(username=&amp;#039;goblinda&amp;#039;,email=&amp;#039;gob@lin.da&amp;#039;)&lt;br /&gt;
session.add(goblinda)&lt;br /&gt;
session.query(User).all()       # Now has an entry =D&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;/div&gt;</summary>
		<author><name>Unhammer</name></author>
	</entry>
</feed>