Skip to content

Instantly share code, notes, and snippets.

@sheki
Created July 22, 2011 10:07
Show Gist options
  • Save sheki/1099194 to your computer and use it in GitHub Desktop.
Save sheki/1099194 to your computer and use it in GitHub Desktop.
SQL Alchemy
>>> from models import *
>>> from database import *
>>> from models import *
>>> teamname='ops'
>>> team = db_session.query(Team).filter_by(name=teamname).one()
>>>
>>> packagename="fk-ops-build
File "<stdin>", line 1
packagename="fk-ops-build
^
SyntaxError: EOL while scanning string literal
>>> packagename="fk-ops-build"
>>> team.addresses.append(Package(name=packagename))
>>> db_session.commit()
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/sqlalchemy/engine/default.py:299: Warning: Field 'id' doesn't have a default value
cursor.execute(statement, parameters)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/sqlalchemy/orm/scoping.py", line 139, in do
return getattr(self.registry(), name)(*args, **kwargs)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/sqlalchemy/orm/session.py", line 614, in commit
self.transaction.commit()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/sqlalchemy/orm/session.py", line 385, in commit
self._prepare_impl()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/sqlalchemy/orm/session.py", line 369, in _prepare_impl
self.session.flush()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/sqlalchemy/orm/session.py", line 1388, in flush
self._flush(objects)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/sqlalchemy/orm/session.py", line 1478, in _flush
flush_context.finalize_flush_changes()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/sqlalchemy/orm/unitofwork.py", line 318, in finalize_flush_changes
self.session._register_newly_persistent(state)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/sqlalchemy/orm/session.py", line 1047, in _register_newly_persistent
'operation.' % mapperutil.state_str(state))
sqlalchemy.orm.exc.FlushError: Instance <Package at 0x538450> has a NULL identity key. Check if this flush is occuring at an inappropriate time, such as during a load operation.
from sqlalchemy import Table, Column, Integer, String, ForeignKey
from sqlalchemy.orm import mapper,relationship
from database import db_session , Base
from sqlalchemy.ext.declarative import declarative_base
class Team( Base ):
__tablename__='teams'
id = Column (Integer, primary_key = True,autoincrement=True)
name = Column(String(50),unique=True)
addresses = relationship ( "Package",backref="team")
class Package(Base):
__tablename__='packages'
id = Column (Integer, primary_key = True,autoincrement=True)
name = Column(String(50),unique=True)
teamid = Column (Integer ,ForeignKey('teams.id'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment