Download
Edit

flaks-SQLALchemy bug

PacoPaco
François Ribémont
Added on 2014-08-24 22:05:17

 

flaks-SQLALchemy bug - Show - Edit - Download

 

# -*- coding: utf-8 -*-
 
import flask
import getpass
 
 
from flask.ext.sqlalchemy import SQLAlchemy
from sqlalchemy.orm import relationship
 
 
app = flask.Flask(__name__)
db = SQLAlchemy(app)
 
# The dabase name
DB_NAME = 'bla'
 
# The database user
# Default: Operatiog System user
# Comment out the getpass class
DB_USER = getpass.getuser()
# And uncomment the next line
# DB_USER = 'paco'
 
# Database password for the database user
DB_PASSWORD = 'bla'
DB_HOST = 'localhost'
 
# Database engine: Uncomment your engine
# Default: postgres
DB_ENGINE = (
    'postgres'
    #'mysql'
    #'sqlite'
)
 
app.secret_key = 'A0Zr98j/3yX R~XHH!jmN]LWX/,?RT'
app.config['SQLALCHEMY_DATABASE_URI'] = (
    '%(DB_ENGINE)s://%(DB_USER)s:%(DB_PASSWORD)s'
    '@%(DB_HOST)s/%(DB_NAME)s' % globals()
)
 
 
class User(db.Model):
    id = db.Column(db.Integer, primary_key=True, autoincrement=1)
    name = db.Column(db.String(80), unique=True)
    accounts = relationship('account', backref='user')
 
 
class Account(db.Model):
    id = db.Column(db.Integer, primary_key=True, autoincrement=1)
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
    name = db.Column(db.String(80), unique=True)
 
if __name__ == '__main__':
    db.create_all()
 
 
 
 
Backtrace:
 
In [1]: import test
 
In [2]: test.User()
---------------------------------------------------------------------------
ArgumentError                             Traceback (most recent call last)
<ipython-input-2-9c2c60694b0b> in <module>()
----> 1 test.User()
 
<string> in __init__(self, **kwargs)
 
/home/paco/Projects/sandeepbox/venv/lib/python3.4/site-packages/sqlalchemy/orm/instrumentation.py in _new_state_if_none(self, instance)
    322                 _new_state_if_none(instance)
    323         else:
--> 324             state = self._state_constructor(instance, self)
    325             setattr(instance, self.STATE_ATTR, state)
    326             return state
 
/home/paco/Projects/sandeepbox/venv/lib/python3.4/site-packages/sqlalchemy/util/langhelpers.py in __get__(self, obj, cls)
    723         if obj is None:
    724             return self
--> 725         obj.__dict__[self.__name__] = result = self.fget(obj)
    726         return result
    727 
 
/home/paco/Projects/sandeepbox/venv/lib/python3.4/site-packages/sqlalchemy/orm/instrumentation.py in _state_constructor(self)
    156     @util.memoized_property
    157     def _state_constructor(self):
--> 158         self.dispatch.first_init(self, self.class_)
    159         return state.InstanceState
    160 
 
/home/paco/Projects/sandeepbox/venv/lib/python3.4/site-packages/sqlalchemy/event/attr.py in __call__(self, *args, **kw)
    258             fn(*args, **kw)
    259         for fn in self.listeners:
--> 260             fn(*args, **kw)
    261 
    262     def __len__(self):
 
/home/paco/Projects/sandeepbox/venv/lib/python3.4/site-packages/sqlalchemy/orm/mapper.py in _event_on_first_init(manager, cls)
   2685     if instrumenting_mapper:
   2686         if Mapper._new_mappers:
-> 2687             configure_mappers()
   2688 
   2689 
 
/home/paco/Projects/sandeepbox/venv/lib/python3.4/site-packages/sqlalchemy/orm/mapper.py in configure_mappers()
   2581                 if not mapper.configured:
   2582                     try:
-> 2583                         mapper._post_configure_properties()
   2584                         mapper._expire_memoizations()
   2585                         mapper.dispatch.mapper_configured(
 
/home/paco/Projects/sandeepbox/venv/lib/python3.4/site-packages/sqlalchemy/orm/mapper.py in _post_configure_properties(self)
   1686 
   1687             if prop.parent is self and not prop._configure_started:
-> 1688                 prop.init()
   1689 
   1690             if prop._configure_finished:
 
/home/paco/Projects/sandeepbox/venv/lib/python3.4/site-packages/sqlalchemy/orm/interfaces.py in init(self)
    142         """
    143         self._configure_started = True
--> 144         self.do_init()
    145         self._configure_finished = True
    146 
 
/home/paco/Projects/sandeepbox/venv/lib/python3.4/site-packages/sqlalchemy/orm/relationships.py in do_init(self)
   1547     def do_init(self):
   1548         self._check_conflicts()
-> 1549         self._process_dependent_arguments()
   1550         self._setup_join_conditions()
   1551         self._check_cascade_settings(self._cascade)
 
/home/paco/Projects/sandeepbox/venv/lib/python3.4/site-packages/sqlalchemy/orm/relationships.py in _process_dependent_arguments(self)
   1603                 util.to_column_set(self.remote_side))
   1604 
-> 1605         self.target = self.mapper.mapped_table
   1606 
   1607     def _setup_join_conditions(self):
 
/home/paco/Projects/sandeepbox/venv/lib/python3.4/site-packages/sqlalchemy/util/langhelpers.py in __get__(self, obj, cls)
    723         if obj is None:
    724             return self
--> 725         obj.__dict__[self.__name__] = result = self.fget(obj)
    726         return result
    727 
 
/home/paco/Projects/sandeepbox/venv/lib/python3.4/site-packages/sqlalchemy/orm/relationships.py in mapper(self)
   1533                 "relationship '%s' expects "
   1534                 "a class or a mapper argument (received: %s)"
-> 1535                 % (self.key, type(argument)))
   1536         return mapper_
   1537 
 
ArgumentError: relationship 'accounts' expects a class or a mapper argument (received: <class 'sqlalchemy.sql.schema.Table'>)