Last active
December 18, 2016 21:35
-
-
Save byronformwalt/10986746fa0e80290b69bcb41e486313 to your computer and use it in GitHub Desktop.
Demonstrating Use of Out-of-Schema Properties with Orator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from orator.migrations import Migration | |
class CreateTableExamples(Migration): | |
def up(self): | |
""" | |
Run the migrations. | |
""" | |
with self.schema.create('examples') as table: | |
table.big_increments('id') | |
table.char('name',255) | |
pass | |
def down(self): | |
""" | |
Revert the migrations. | |
""" | |
self.schema.drop_if_exists('hands') | |
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from orator import Model | |
class Example(Model): | |
# Exclude timestamps. | |
__timestamps__ = [] | |
def __init__(self,c=None): | |
super(Example, self).__init__() | |
self.__name = "This world is so confusing from the inside!" # Commenting this line will make saving possible. | |
self.name = "Hello World!" | |
def __str__(self): | |
return self.name | |
def __repr__(sself): | |
return self.__name |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Define how orator's CLI should connect to the database. | |
databases: | |
postgresql: | |
driver: postgres | |
host: localhost | |
database: gist_examples |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# Configure the database connection, and make a connection. | |
config = { | |
'postgresql': { | |
'driver': 'postgres', | |
'host': 'localhost', | |
'database': 'gist_examples' | |
} | |
} | |
from orator import DatabaseManager | |
db = DatabaseManager(config) | |
# Bind the Model base class to the database connection. | |
from orator import Model | |
Model.set_connection_resolver(db) | |
# Import the Example model class. | |
from example_model import Example | |
# Create an instance of the Example model class. | |
example = Example() | |
print("==> Here is my example: '{}'\n".format(example)) | |
# Attempt to save the instance as a new record in the database. | |
# This generates an error because we defined a property that | |
example.save() | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Create the database if it does not already exist. | |
if psql -lqt | cut -d \| -f 1 | grep -qw "gist_examples"; then | |
# Do nothing. | |
echo "Database 'gist_examples' already exists." | |
else | |
# Create the database, since it does not already exist. | |
echo "Creating database 'gist_examples'." | |
psql -c "CREATE DATABASE ""gist_examples"";" | |
fi | |
# Run orator migrations interactively. | |
MIGRATION_PATH=./ | |
orator migrate --path=$MIGRATION_PATH | |
Here is the error that appears;
localhost:orator_gist formwalt$ ./setup_example.bash
Database 'gist_examples' already exists.
Are you sure you want to proceed with the migration? y
[OK] Migrated 2016_12_18_205220_create_table_examples
localhost:orator_gist formwalt$ ./
__pycache__/ run_example.py setup_example.bash
localhost:orator_gist formwalt$ ./run_example.py
==> Here is my example: 'Hello World!'
localhost:orator_gist formwalt$ ./run_example.py
==> Here is my example: 'Hello World!'
Traceback (most recent call last):
File "/Applications/anaconda/lib/python3.5/site-packages/orator/connections/connection.py", line 31, in _run
result = wrapped(self, query, bindings, *args, **kwargs)
File "/Applications/anaconda/lib/python3.5/site-packages/orator/connections/connection.py", line 199, in select
cursor.execute(query, bindings)
File "/Applications/anaconda/lib/python3.5/site-packages/psycopg2/extras.py", line 120, in execute
return super(DictCursor, self).execute(query, vars)
psycopg2.ProgrammingError: column "_Example__name" of relation "examples" does not exist
LINE 1: INSERT INTO "examples" ("_Example__name", "name") VALUES ('T...
^
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "./run_example.py", line 27, in <module>
example.save()
File "/Applications/anaconda/lib/python3.5/site-packages/orator/orm/model.py", line 1526, in save
saved = self._perform_insert(query, options)
File "/Applications/anaconda/lib/python3.5/site-packages/orator/orm/model.py", line 1597, in _perform_insert
self._insert_and_set_id(query, attributes)
File "/Applications/anaconda/lib/python3.5/site-packages/orator/orm/model.py", line 1619, in _insert_and_set_id
id = query.insert_get_id(attributes, key_name)
File "/Applications/anaconda/lib/python3.5/site-packages/orator/orm/builder.py", line 1155, in call
result = attribute(*args, **kwargs)
File "/Applications/anaconda/lib/python3.5/site-packages/orator/query/builder.py", line 1409, in insert_get_id
return self._processor.process_insert_get_id(self, sql, values, sequence)
File "/Applications/anaconda/lib/python3.5/site-packages/orator/query/processors/postgres_processor.py", line 27, in process_insert_get_id
result = query.get_connection().select_from_write_connection(sql, values)
File "/Applications/anaconda/lib/python3.5/site-packages/orator/connections/connection.py", line 190, in select_from_write_connection
return self.select(query, bindings)
File "/Applications/anaconda/lib/python3.5/site-packages/orator/connections/connection.py", line 34, in _run
e, query, bindings, wrapped
File "/Applications/anaconda/lib/python3.5/site-packages/orator/connections/connection.py", line 317, in _try_again_if_caused_by_lost_connection
raise QueryException(query, bindings, e)
orator.exceptions.query.QueryException: column "_Example__name" of relation "examples" does not exist
LINE 1: INSERT INTO "examples" ("_Example__name", "name") VALUES ('T...
^
(SQL: INSERT INTO "examples" ("_Example__name", "name") VALUES (%s, %s) RETURNING "id" (['This world is so confusing from the inside!', 'Hello World!']))
localhost:orator_gist formwalt$
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This example assumes that you have orator, python3, and PostgreSQL installed on your local posix-compliant machine. Please, enable execution permissions on
setup_example.bash
andrun_example.py
before proceeding. To run the example, illustrating the error that occurs when defining out-of-schema properties on an orator model, run execute the following from the same directory where you have saved the gist files:To verify that everything has been correctly setup, comment out line 10 from
example_model.py
and then rerun the following command:If you do not see any errors, then everything has been setup correctly.