coleifer / peewee

a small, expressive orm -- supports postgresql, mysql, sqlite and cockroachdb
http://docs.peewee-orm.com/
MIT License
11.2k stars 1.37k forks source link

Peewee with Google Cloud SQL #183

Closed moraes closed 11 years ago

moraes commented 11 years ago

For the record, here's a recipe to use Peewee with Google Cloud SQL on App Engine Python SDK. Cloud SQL uses MySql, so the only difference is the connection:

from google.appengine.api import rdbms
import peewee

class AppEngineDatabase(peewee.MySQLDatabase):
    def _connect(self, database, **kwargs):
        if 'instance' not in kwargs:
            raise peewee.ImproperlyConfigured('Missing "instance" keyword to connect to database')
        return rdbms.connect(database=database, **kwargs)

It works like a charm.

coleifer commented 11 years ago

Hmm...I wonder where a good place would be to put this information? Having not tested cloud sql myself I'm reluctant to add something like this to "playhouse", but it does seem like useful information to put somewhere...Maybe I'll add a new section to the docs.

coleifer commented 11 years ago

http://peewee.readthedocs.org/en/latest/peewee/database.html#peewee-and-other-databases

Thanks @moraes

jpttrssn commented 10 years ago

Google recommends using MySQLdb over their rdms driver whenever possible. See this page of their documentation: https://developers.google.com/appengine/docs/python/cloud-sql/ Below is what I ended up doing to get peewee to work in both development and production. Along with the changes in my fork of peewee.

if (os.getenv('SERVER_SOFTWARE') and
        os.getenv('SERVER_SOFTWARE').startswith('Google App Engine/')):
    db = MySQLDatabase('<db_name>', unix_socket='/cloudsql/<instance_name>', user='root')
else:
    db = MySQLDatabase('<db_name>', user='user', passwd='password')