jpsca / sqla-wrapper Goto Github PK
View Code? Open in Web Editor NEWA friendly wrapper for modern SQLAlchemy and Alembic
Home Page: https://sqla-wrapper.scaletti.dev
License: MIT License
A friendly wrapper for modern SQLAlchemy and Alembic
Home Page: https://sqla-wrapper.scaletti.dev
License: MIT License
Orignal Query -
data = db.session.query(PublishEvent, ImageLicense).join(PublishEvent.images).join(ImageLicense).filter(ImageLicense.licensed_at != None')
Traceback -
/Users/newscred/.virtualenvs/nc-reporting/lib/python2.7/site-packages/IPython/core/displayhook.pyc in __call__(self, result)
245 self.start_displayhook()
246 self.write_output_prompt()
--> 247 format_dict, md_dict = self.compute_format_data(result)
248 self.write_format_data(format_dict, md_dict)
249 self.update_user_ns(result)
/Users/newscred/.virtualenvs/nc-reporting/lib/python2.7/site-packages/IPython/core/displayhook.pyc in compute_format_data(self, result)
155
156 """
--> 157 return self.shell.display_formatter.format(result)
158
159 def write_format_data(self, format_dict, md_dict=None):
/Users/newscred/.virtualenvs/nc-reporting/lib/python2.7/site-packages/IPython/core/formatters.pyc in format(self, obj, include, exclude)
150 md = None
151 try:
--> 152 data = formatter(obj)
153 except:
154 # FIXME: log the exception
/Users/newscred/.virtualenvs/nc-reporting/lib/python2.7/site-packages/IPython/core/formatters.pyc in __call__(self, obj)
479 type_pprinters=self.type_printers,
480 deferred_pprinters=self.deferred_printers)
--> 481 printer.pretty(obj)
482 printer.flush()
483 return stream.getvalue()
/Users/newscred/.virtualenvs/nc-reporting/lib/python2.7/site-packages/IPython/lib/pretty.pyc in pretty(self, obj)
345 if cls in self.type_pprinters:
346 # printer registered in self.type_pprinters
--> 347 return self.type_pprinters[cls](obj, self, cycle)
348 else:
349 # deferred printer
/Users/newscred/.virtualenvs/nc-reporting/lib/python2.7/site-packages/IPython/lib/pretty.pyc in inner(obj, p, cycle)
529 p.text(',')
530 p.breakable()
--> 531 p.pretty(x)
532 if len(obj) == 1 and type(obj) is tuple:
533 # Special case for 1-item tuples.
/Users/newscred/.virtualenvs/nc-reporting/lib/python2.7/site-packages/IPython/lib/pretty.pyc in pretty(self, obj)
345 if cls in self.type_pprinters:
346 # printer registered in self.type_pprinters
--> 347 return self.type_pprinters[cls](obj, self, cycle)
348 else:
349 # deferred printer
/Users/newscred/.virtualenvs/nc-reporting/lib/python2.7/site-packages/IPython/lib/pretty.pyc in inner(obj, p, cycle)
529 p.text(',')
530 p.breakable()
--> 531 p.pretty(x)
532 if len(obj) == 1 and type(obj) is tuple:
533 # Special case for 1-item tuples.
/Users/newscred/.virtualenvs/nc-reporting/lib/python2.7/site-packages/IPython/lib/pretty.pyc in pretty(self, obj)
360 if callable(meth):
361 return meth(obj, self, cycle)
--> 362 return _default_pprint(obj, self, cycle)
363 finally:
364 self.end_group()
/Users/newscred/.virtualenvs/nc-reporting/lib/python2.7/site-packages/IPython/lib/pretty.pyc in _default_pprint(obj, p, cycle)
480 if getattr(klass, '__repr__', None) not in _baseclass_reprs:
481 # A user-provided repr.
--> 482 p.text(repr(obj))
483 return
484 p.begin_group(1, '<')
UnicodeEncodeError: 'ascii' codec can't encode character u'\u2019' in position 63: ordinal not in range(128)
As we know it's base on SQLAlchemy 1.4 from 5.0.0, Asynchronous I/O is theoretically supported.
Would you provide some example using in sqla-wrapper
?
Hello,
My personal project need custom metaclass.
As I know, it already merged. When will it release ?
As i am trying to make the "transition" from Flask-SQLAlchemy to your fork i found this problem where the debug toolbar doesnt show information about the queries, it just tells me that i have to install Flask-SQLAlachemy.
I saw a commit about supporting the toolbar in your fork but im not expert to debug what is wrong cause maybe im missing something.
# __init__.py
from flask import Flask
from sqlalchemy_wrapper import SQLAlchemy
from flask.ext.bcrypt import Bcrypt
app = Flask(__name__, instance_relative_config=True, static_url_path='')
bcrypt = Bcrypt(app)
app.config.from_pyfile('development.py')
db = SQLAlchemy(app.config['SQLALCHEMY_DATABASE_URI'], app=app)
# run.py
from flask_debugtoolbar import DebugToolbarExtension
toolbar = DebugToolbarExtension()
toolbar.init_app(app)
Partial of the traceback:
...
db = SQLAlchemy(
^^^^^^^^^^^
File "/home/ubuntu/workspace/nb/.venv/lib/python3.11/site-packages/sqla_wrapper/sqlalchemy_wrapper.py", line 94, in init
self._include_sqlalchemy()
File "/home/ubuntu/workspace/nb/.venv/lib/python3.11/site-packages/sqla_wrapper/sqlalchemy_wrapper.py", line 126, in _include_sqlalchemy
for key in module.all:
^^^^^^^^^^^^^^
AttributeError: module 'sqlalchemy' has no attribute 'all'. Did you mean: 'file'?
sqlalchemy_wrapper.py line 82-83
self.Session = sqlalchemy.orm.sessionmaker(**session_options)
self.s = PatchedScopedSession(self.Session)
class PatchedScopedSession(scoped_session):
pass
The argument pass to PatchedScopedSession
is naming scoped_session, but maybe it's not scoped session, it's default session.
Because db.Model
explicitly defines __tablename__
, STI doesn't work since the declarative_base
expects __tablename__
to be undefined to work properly.
flask-sqlalchemy actually had the same problem at one point and they fixed this by checking to see whether the table name should actually be set in different instances: https://github.com/mitsuhiko/flask-sqlalchemy/blob/master/flask_sqlalchemy/__init__.py#L553-L591
Quickstart show examples like this
users = User.query.all()
But that syntax is not supported yet, the correct one should be:
users = db.query(User).all()
https://github.com/jpscaletti/sqla-wrapper/blob/master/docs/quickstart.rst#simple-relationships
Quickstart provides example:
users = User.query.all()
but actually I am getting error:
AttributeError: type object 'User' has no attribute 'query'
Hi!
There are two Pypi packages for this project:
The latter is probably the correct one (or the latest one), but having two pip packages can confuse users. Please either delete the SQLAlchemy-Wrapper
package or add a notice in the README.md file to install sqla-wrapper
.
Usually readme files start with installation instructions: pip install sqla-wrapper
. This makes it clear which package to install.
When trying to migrate my project to sqlalchemy-wrapper, I have spotted incomplete imports of useful functions for declarative model:
from sqlalchemy_wrapper import SQLAlchemy
db = SQLAlchemy(...)
db.relationship # works
db.association_proxy # not found
from sqlalchemy.ext.associationproxy import association_proxy # works - provided by vanilla SQLAlchemy
Could you please add the association_proxy to sqlalchemy_wrapper.SQLAlchemy?
It's good to have example of running unit tests using test database and sqlalchemy.
Direct adoption of following article - now working.
http://kronosapiens.github.io/blog/2014/07/29/setting-up-unit-tests-with-flask.html
In model.py
after cls.__tablename__ = _get_table_name(cls.__name__)
,
would you consider adding
cls.__table_args__ = {'extend_existing':True}
?
It allows to change the model interactively, e.g. in IPython.
I am trying to initialize the database through a factory function as i have circular import problems.
# __init__.py
db = SQLAlchemy()
def create_app(config):
app = Flask(__name__, instance_relative_config=True, static_url_path='')
# Didnt test if Toolbar works with this way yet but anyway..
toolbar = DebugToolbarExtension(app)
app.config.from_object(config)
if 'APP_CONFIG_FILE' in os.environ:
app.config.from_envvar('APP_CONFIG_FILE')
db.init_app(app)
# config.py
from os.path import dirname, abspath
class DefaultConfiguration:
DATABASE = 'my_database.db'
SQLALCHEMY_DATABASE_URI = ('sqlite:///' + dirname(abspath(__file__)) +
'/' + DATABASE)
TESTING = False
DEBUG = False
DEBUG_TB_INTERCEPT_REDIRECTS = False
RECORD_QUERIES = False
SECRET_KEY = 'Basic_key_thats_supposed_to_be_secret'
BCRYPT_LEVEL = 12
USERNAME = 'admin'
PASSWORD = 123
I tried to pdb and everytime db shows sqlite:// (memory) even though app.config shows correct information about DATABASE_URI and DATABASE. I tried to initialize before and after the configuration fo the app(app.config.from_object).
I am not expert what so ever so i might miss something!
How would one use sql-wrapper with existing db?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.