flask-sqlalchemy

Flask Sqlalchemy : relationships between different modules

被刻印的时光 ゝ 提交于 2019-11-28 01:30:02
问题 I'm following the Flask-SQLAlchemy tutorial. I have Flask 0.9, sqlalchemy 0.7.8 and flask-sqlalchemy 0.16 on python 2.6. I'm trying to create a "one to many" relationship, like in their tutorial. class Person(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(50)) addresses = db.relationship('Address', backref='person', lazy='dynamic') class Address(db.Model): id = db.Column(db.Integer, primary_key=True) email = db.Column(db.String(50)) person_id = db.Column(db

how to use QuerySelectField in flask?

核能气质少年 提交于 2019-11-27 23:07:22
I am trying to have a select field filled with the results of a sqlalchemy request in a flask form. Here are the code : def possible_book(): return Book.query.with_entities(Book.id).all() class AuthorForm(Form): familyname = TextField('familyname', [validators.Required()]) firstname = TextField('firstname', [validators.Required()]) book_list = QuerySelectField('book_list',query_factory=possible_book,get_label='title',allow_blank=True) This is the template : <form action="" method="post" name="edit_author" enctype="multipart/form-data"> {{form.hidden_tag()}} <p>Veuillez donner son prénom

Flask-SQLAlchemy check if row exists in table

假如想象 提交于 2019-11-27 20:07:41
I have a Flask application which uses Flask-SQLAlchemy to connect to a MySQL database. I would like to be able to check whether a row is present in a table. How would I modify a query like so to check the row exists: db.session.query(User).filter_by(name='John Smith') I found a solution on this question which uses SQLAlchemy but does not seem to fit with the way Flask-SQLAlchemy works: from sqlalchemy.sql import exists print session.query(exists().where(User.email == '...')).scalar() Thanks. Since you only want to see if the user exists, you don't want to query the entire object. Just query

AttributeError: 'int' object has no attribute '_sa_instance_state'

安稳与你 提交于 2019-11-27 19:36:31
I'm working on forum template using Flask. When I attempt creating a new thread in the browser using forms, SQLAlchemy throws an AttributeError. The problem showed up when I tried implementing a one-to-many relationship with Forum-to-Thread and a one-to-many relationship with Thread-to-User. models.py class User(db.Model): id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(32), index=True, unique=True) password = db.Column(db.String(32), index=True) email = db.Column(db.String(120), index=True, unique=True) role = db.Column(db.SmallInteger, default=ROLE_USER) posts =

Flask sqlalchemy many-to-many insert data

你离开我真会死。 提交于 2019-11-27 17:17:14
I am trying to make a many to many relation here in Flask-SQLAlchemy , but it seems that I don't know how to fill the "many to many identifier database" . Could you please help me understand what I am doing wrong and how it is supposed to look? class User(db.Model): __tablename__ = 'users' user_id = db.Column(db.Integer, primary_key=True) user_fistName = db.Column(db.String(64)) user_lastName = db.Column(db.String(64)) user_email = db.Column(db.String(128), unique=True) class Class(db.Model): __tablename__ = 'classes' class_id = db.Column(db.Integer, primary_key=True) class_name = db.Column(db

Flask-SQLAlchemy - Greater than or equal to

会有一股神秘感。 提交于 2019-11-27 17:05:58
问题 I'm having trouble figuring out how to do a "greater than or equal to" comparison in a query. I have a model field: invoicedate = db.Column(db.Date(), nullable=True, key='InvoiceDate') And i'm trying to do the following filter: Invoice.query.filter_by(invoicedate >= date.today()).count() When I run the view, it keeps throwing the following error: NameError: global name 'invoicedate' is not defined What is the correct syntax for a greater than or equal filter in sqlalchemy or flask-sqlalchemy?

SQL-alchemy: ValueError too many values to unpack?

五迷三道 提交于 2019-11-27 17:01:51
问题 I have a website running with a mysql database using the sql-alchemy package that has suddenly broken. I have done some research and found that the expected issue is that the newest sql-alchemy update is handing flask-admin one more value than expected from "cls, key = identity_key(instance=obj)" Source The suggested fix is to edit the files to accept a third item but I am unable to do this with the permissions I have on my environment. Another answer links to a repo on github but I cannot

Alembic --autogenerate producing empty migration

落爺英雄遲暮 提交于 2019-11-27 15:47:30
问题 I am trying to use Alembic for the first time and want to use --autogenerate feature described here My project structure looks like project/ configuration/ __init__.py dev.py test.py core/ app/ models/ __init__.py user.py db/ alembic/ versions/ env.py alembic.ini I am using Flask and SQLAlchemy and their Flask-SQLAlchemy extension. my model User looks like class User(UserMixin, db.Model): __tablename__ = 'users' # noinspection PyShadowingBuiltins uuid = Column('uuid', GUID(), default=uuid

Flask-SQLAlchemy - how do sessions work with multiple databases?

半世苍凉 提交于 2019-11-27 14:04:19
问题 I'm working on a Flask project and I am using Flask-SQLAlchemy. I need to work with multiple already existing databases. I created the "app" object and the SQLAlchemy one: from flask import Flask from flask_sqlalchemy import SQLAlchemy app = Flask(__name__) db = SQLAlchemy(app) In the configuration I set the default connection and the additional binds: SQLALCHEMY_DATABASE_URI = 'postgresql://pg_user:pg_pwd@pg_server/pg_db' SQLALCHEMY_BINDS = { 'oracle_bind': 'oracle://oracle_user:oracle_pwd

SQLAlchemy query, join on relationship and order by count

别等时光非礼了梦想. 提交于 2019-11-27 14:03:32
问题 I have two SQLAlchemy models set up as follows: ############## # Post Model # ############## class Post(db.Model): id = db.Column(db.Integer, primary_key = True) title = db.Column(db.String(250)) content = db.Column(db.String(5000)) timestamp = db.Column(db.Integer) author_id = db.Column(db.Integer, db.ForeignKey('user.id')) likes = db.relationship('Like', backref = 'post', lazy = 'dynamic') ############### # Likes Model # ############### class Like(db.Model): id = db.Column(db.Integer,