flask-sqlalchemy

flask-admin not showing foreignkey columns

梦想与她 提交于 2019-12-05 02:38:39
class Parent(db.Model): id = db.Column(db.Integer, primary_key = True) name = db.Column(db.String(120)) def __repr_(self): return '<Parent %r>' % (self.name) admin.add_view(ModelView(Parent, db.session)) class Child(db.Model): id = db.Column(db.Integer, primary_key = True) name = db.Column(db.String(120)) parent = db.Column(db.Integer, db.ForeignKey(Parent)) admin.add_view(ModelView(Child, db.session)) Hello - The code above is an example of flask-admin page that I am trying to create. The goal is to have on Child's create page a text box for name and a drop down to select a parent. With the

Flask-SQLAlchemy and Flask-Restless not fetching grandchildren

久未见 提交于 2019-12-05 01:39:13
问题 Problem I am building an app on Flask, Flask-SQLAlchemy, and Flask-Restless. I have used restless to generate an API for a parent-child-grandchild relationship*. A GET on my child will correctly fetch the grandchild, but a GET on the parent will not fetch the grandchild for each child. *In fact, the parent-child relationship is a many-to-many, but same premise. Models class Grandchild(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String, nullable=False) parent =

SQLAlchemy left join using subquery

梦想与她 提交于 2019-12-05 01:32:25
问题 Lets say there's a table "posts" which contains blog posts, and another table "favorites" which links a username to a post. Many users can favorite a post, so the relationship is one post to many favorites. I am trying to figure out the syntax to join posts to favorites, but I only want those favorites where the user is the current user. I want some thing like: current_user = 'testuser' posts.query.outerjoin(favorites, and_(posts.post_id == favorites.post_id, favorites.username == current

SQLAlchemy / Flask / PostgreSQL pool connection

独自空忆成欢 提交于 2019-12-05 00:35:31
After having played for a long time with Django, I'm trying a bit of Flask with SQLAlchemy, and I must say I quite like it. However there is something that I don't get: I have a small Flask / SQLAlchemy app that uses PostgreSQL. In my __init__.py file I have: from flask import Flask from flask.ext.sqlalchemy import SQLAlchemy app = Flask(__name__) app.config.from_object('settings') db = SQLAlchemy(app) I wanted to know: Is there automatic connection pooling ? Or does every call to Model.query... create a new connection to the database? If not, how can I configure it? If yes, what are the

Questions about using AWS RDS environment variables to connect to my AWS RDS database

廉价感情. 提交于 2019-12-05 00:11:43
问题 In my Flask-SQLAlchemy application hosted on AWS, I take advantage of environment variables added by the RDS instance I have associated with my applications environment by construction my database connection URI with application.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://{}:{}@{}:{}/{}'.format( os.environ['RDS_USERNAME'], os.environ['RDS_PASSWORD'], os.environ['RDS_HOSTNAME'], os.environ['RDS_PORT'], os.environ['RDS_DB_NAME']) This works fine but leaves me with several issues I'm

SQLAlchemy Object already attached to session

左心房为你撑大大i 提交于 2019-12-04 23:50:20
I'm trying to get a server for an app working, but I'm getting an error upon login: [!] Object '<User at 0x7f12bc185a90>' is already attached to session '2' (this is '3') It seems the session I'm adding is already on the database. This is the snippet of code that is causing the problem: @app.route('/login', methods=['POST']) def login(): u = User.query.filter(User.username == request.form["username"]).first() if not u or u.password != request.form["password"]: return error("E1") s = Session.get_by_user(u) if s is not None: db_session.delete(s) db_session.commit() print db_session.execute(

SqlAlchemy won't accept datetime.datetime.now value in a DateTime column

自作多情 提交于 2019-12-04 23:45:20
I should first mention that I'm using SqlAlchemy through Flask-SqlAlchemy. I don't believe this affects the issue but if it does, please let me know. Here is the relevant part of the error message I'm getting when running the create_all function in SqlAlchemy InterfaceError: (InterfaceError) Error binding parameter 4 - probably unsupported type. u'INSERT INTO podcasts (feed_url, title, url, last_updated, feed_data) VALUES (?, ?, ?, ?, ?)' (u'http://example.com/feed', u'Podcast Show Title', u'http://example.com', '2012-04-17 20:28:49.117000' Here is my model: class Podcast(db.Model): import

flask-migrate doesn't work When I add models with ForeignKey

橙三吉。 提交于 2019-12-04 22:00:15
class User(db.Model): __tablename__ = 'user' id = db.Column(db.Integer, primary_key=True) email = db.Column(db.String(64), unique=True) # 是不是应该加密下,不能明文存储?应该设置多长的空间? 14.7.18 4:22 by lee password = db.Column(db.String(100)) nickname = db.Column(db.String(64)) school = db.Column(db.String(20)) sex = db.Column(db.String(5)) status = db.Column(db.String(10)) grade = db.Column(db.String(18)) I have a database remains. Then I add model to models.py: class PubSquare(db.Model): id = db.Column(db.Integer, primary_key=True) author_id = db.Column(db.Integer, db.ForeignKey('user.id')) author = db

How can SQLAlchemy association_proxy be used bi-directionally?

大城市里の小女人 提交于 2019-12-04 19:46:42
I'm working with Flask-SQLAlchemy to create a many-to-many relationship using Association Objects and association_proxy , but I'm running into an issue: When I use association_proxy , I can set it up such that when I append a new instance to one side of the relationship, the Association Object will be created, but if I attempt to add an instance from the other side of the relationship, the Association Object constructor won't be passed the correct instance. Here's a simplified example where Club has many People , and People have many Club , and the Association Object is a Membership : class

Alembic: How to migrate custom type in a model?

心已入冬 提交于 2019-12-04 19:00:24
问题 My User model is class User(UserMixin, db.Model): __tablename__ = 'users' # noinspection PyShadowingBuiltins uuid = Column('uuid', GUID(), default=uuid.uuid4, primary_key=True, unique=True) email = Column('email', String, nullable=False, unique=True) _password = Column('password', String, nullable=False) created_on = Column('created_on', sa.types.DateTime(timezone=True), default=datetime.utcnow()) last_login = Column('last_login', sa.types.DateTime(timezone=True), onupdate=datetime.utcnow())