在现有的数据库周围烧瓶sqlalchemy示例 [英] flask sqlalchemy example around existing database
问题描述
我喜欢这里,但我似乎无法让它在Flask中工作(我可以使用sqlalchemy来查询每个例子)。
我从上面的例子中建立的model.py结果是
编辑我拉着行
db.Model.metadata.reflect [db.engine]
$ c $从另一个帖子,它应该是db.Model.metadata.reflect(db.engine)
非常简单的解决方案
>
这里是我的model.py
from应用程序从sqlalchemy.orm导入db
导入关系
$ b $ db.Model.metadata.reflect [db.engine] #change到(db.engine)
class建筑物(db.Model):
__table__ = db.Model.metadata.tables ['test']
__bind_key__ ='chet'
def __repr __(self):
return self .test1
....来自sqlalchemy uri的其他模型在这里...
我得到这个
>>> from app import db,models
Traceback(最近一次调用的最后一个):
在< module>中,第1行的文件< stdin>
在< module>文件中的第69行app / __ init__.py应用程序导入视图中的
,模型
在< module>中的文件app / views.py,第1行
从应用程序导入应用程序,模型,db
在< module>文件中的app / models.py
db.Model.metadata.reflect [db.engine]
TypeError:'instancemethod'对象没有属性'__getitem__'
这是我的config.py
SQLALCHEMY_DATABASE_URI =' postgresql:// chet @ localhost / ubuntuweb'
SQLALCHEMY_BINDS = {
'chet':'postgresql:// chet @ localhost / warehouse',
}
这里是我的init.py文件
从烧瓶导入烧瓶
从flask_bootstrap导入引导$ b $从flask.ext.sqlalchemy导入
从flask.ext.admin导入SQLAlchemy
导入Admin,BaseView ,从flask.ext.script中导入
从flask.ext.migrate导入管理器
导入从flask.ext.login迁移,迁移命令
导入LoginManager,UserMixin,login_required
app = Flask(__ name__)
app.config ['UPLOAD_FOLDER'] ='app / static'
app.config.from_object('config')
db = SQLAlchemy(app)
migrate =迁移(app,db)
manager = Manager(app)
manager.add_command('db',MigrateCommand)
Bootstrap(应用程序)
从应用程序导入视图,模型
admin =管理(应用程序)
你的代码有
$ $ p $ db.Model.metadata。 反映[db.engine]
何时应该是
db.Model.metadata.reflect(db.engine)#parens not brackets
这应该是从堆栈跟踪中显而易见的....
Problem: there needs to be full working example of auto-mapping sqlalchemy to an existing database in an app with multiple binds.
I want to bind to two databases and have one auto-map the tables. I need to do this because i don't have control over one DB, thus would have to constantly re-write my models and might delete new tables every time i migrate.
I love the answers given here, but i can't seem to make it work in Flask (I am able to use the sqlalchemy only to query as per the example).
The model.py I set up from the example above results in
EDIT I pulled the line
db.Model.metadata.reflect[db.engine]
from another post, and it should be db.Model.metadata.reflect(db.engine) very simple solution
here is my model.py
from app import db
from sqlalchemy.orm import relationship
db.Model.metadata.reflect[db.engine]#change to (db.engine)
class Buildings(db.Model):
__table__ = db.Model.metadata.tables['test']
__bind_key__ = 'chet'
def __repr__(self):
return self.test1
.... other models from sqlalchemy uri here...
i get this
>>> from app import db, models
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "app/__init__.py", line 69, in <module>
from app import views, models
File "app/views.py", line 1, in <module>
from app import app,models, db
File "app/models.py", line 163, in <module>
db.Model.metadata.reflect[db.engine]
TypeError: 'instancemethod' object has no attribute '__getitem__'
Here's my config.py
SQLALCHEMY_DATABASE_URI = 'postgresql://chet@localhost/ubuntuweb'
SQLALCHEMY_BINDS = {
'chet': 'postgresql://chet@localhost/warehouse',
}
here's my init.py file
from flask import Flask
from flask_bootstrap import Bootstrap
from flask.ext.sqlalchemy import SQLAlchemy
from flask.ext.admin import Admin, BaseView, expose
from flask.ext.script import Manager
from flask.ext.migrate import Migrate, MigrateCommand
from flask.ext.login import LoginManager, UserMixin, login_required
app = Flask(__name__)
app.config['UPLOAD_FOLDER'] = 'app/static'
app.config.from_object('config')
db = SQLAlchemy(app)
migrate = Migrate(app, db)
manager = Manager(app)
manager.add_command('db', MigrateCommand)
Bootstrap(app)
from app import views, models
admin = Admin(app)
Your code has
db.Model.metadata.reflect[db.engine]
When it should be
db.Model.metadata.reflect(db.engine) # parens not brackets
This should have been pretty obvious from the stack trace....
这篇关于在现有的数据库周围烧瓶sqlalchemy示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!