flask-sqlalchemy

Flask-SQLAlchemy: multiple filters through one relation

邮差的信 提交于 2019-12-06 02:44:05
问题 I have two models, Tags and Photos, that have a many-to-many-relationship like so: tag_identifier = db.Table('tag_identifier', db.Column('photo_id', db.Integer, db.ForeignKey('photo.id')), db.Column('tag_id', db.Integer, db.ForeignKey('tag.id')) ) class Tag(db.Model): id = db.Column(db.Integer, primary_key=True) class Photo(db.Model): id = db.Column(db.Integer, primary_key=True) tags = db.relationship('Tag', secondary=tag_identifier, backref=db.backref('photos', lazy='dynamic'), lazy='dynamic

Flask SqlAlchemy join two models without foreign key MYSQL

让人想犯罪 __ 提交于 2019-12-06 01:52:43
问题 I am joining two models without a foreign key: Models: class Users(db.Model): __tablename__ = "Users" userName = db.Column(db.String, primary_key=True) lastLogin = db.Column(db.DateTime) class TimeOff __tablename__ = "timeOff" timeOffID = db.Column(db.Integer, primary_key=True) userName = db.Column("userName", db.String, db.ForeignKey('appUsers.userName')), dayWork = db.Column(db.DateTime) View: result = db.session.query(models.Users).join(models.TimeOff) sqlalchemy.exc.InvalidRequestError:

Batch Editing in Flask-Admin

拟墨画扇 提交于 2019-12-06 01:35:41
I'm using Flask-Admin and I want to be able to update many fields at once from the list view. It seemed like what I'm looking for is a custom action. I was able to make it work, but I suspect not in the best way. I'm wondering if it could be done more "Flask"-ily. What I do now, for example if I was updating all rows in table cars to have tires = 4 : A custom action in the CarView class collects the ids of the rows to be modified, a callback url from request.referrer , and the tablename cars , and returns render_template(mass_update_info.html) with these as parameters. mass_update_info.html is

How to return “already exists” error in Flask-restless?

拜拜、爱过 提交于 2019-12-06 01:31:23
问题 I would like to do some handler for exception. I'm using a combination of Flask-restless and SQLAlchemy in python. My problem: When I send request to api with object that already exists in DB, SQLAlchemy shows exception: IntegrityError: (IntegrityError) column <column_name> is not unique u'INSERT INTO ... So I have tried to add attribute validation_exceptions into create_api method: manager.create_api( ... , validation_exceptions=[IntegrityError]) But response json contains: { "validation

Auto incrementing a non-unique id upon creation using SQLAlchemy

限于喜欢 提交于 2019-12-06 01:04:10
My primary goal with this is to make implementing revision histories and journaling easier. I found myself wondering if it's possible, using Flask-SQLAlchemy (or just straight up SQL), to get an auto incremented non-unique integer for mysql. I found this stack overflow post which is close to what I want to do but the question is focused on a primary key. For example, if my table had these columns, revision_id = db.Column(db.Integer, nullable=False) post_id = db.Column(db.Integer, nullable=False) __table_args__ = ( PrimaryKeyConstraint('post_id', 'revision_id'), ) Would it be possible to create

Sqlalchemy one to many relationship join?

折月煮酒 提交于 2019-12-05 20:19:17
I am trying to do a simple join query like this, SELECT food._id, food.food_name, food_categories.food_categories FROM food JOIN food_categories ON food.food_category_id = food_categories._id but keep receiving an error. Here is how my classes are setup. class Food_Categories(db.Model): __tablename__ = 'food_categories' _id = db.Column(db.Integer, primary_key=True) food_categories = db.Column(db.String(30)) class Food(db.Model): __tablename__ = 'food' _id = db.Column(db.Integer, primary_key=True) food_name = db.Column(db.String(40)) food_category_id = db.Column(db.Integer, ForeignKey(Food

Pass encoding parameter to cx_oracle from sqlalchemy

强颜欢笑 提交于 2019-12-05 18:43:59
I'm using Oracle Database with UTF-16 encoding. The diacritics is correctly displayed when using directly cx_oracle client. Connection statement is like this: cx_Oracle.connect(username, password, conn_str, encoding='UTF-16', nencoding='UTF-16') However, now I'm building bigger application and I would like to use SQLalchemy in Flask . Code looks like this: from flask_sqlalchemy import SQLAlchemy db = SQLAlchemy(app) db.Model.metadata.reflect(db.engine) class MyTable(db.Model): __table__ = db.Model.metadata.tables['mytable'] for row in MyTable.query: print(row.column_with_diacritics) Output of

How to query JSON Array in Postgres with SqlAlchemy?

拈花ヽ惹草 提交于 2019-12-05 14:54:54
I have a SqlAlchemy model defined from sqlalchemy.dialects.postgresql import JSONB class User(db.Model): __tablename__ = "user" id = db.Column(db.Integer, primary_key=True) nickname = db.Column(db.String(255), nullable=False) city = db.Column(db.String(255)) contact_list = db.Column(JSONB) created_at = db.Column(db.DateTime, default=datetime.utcnow) def add_user(): user = User(nickname="Mike") user.contact_list = [{"name": "Sam", "phone": ["123456", "654321"]}, {"name": "John", "phone": ["159753"]}, {"name": "Joe", "phone": ["147889", "98741"]}] db.session.add(user) db.session.commit() if _

Class does not have a table or tablename specified and does not inherit from an existing table-mapped class

左心房为你撑大大i 提交于 2019-12-05 13:51:39
问题 When I tried to add a new table to python/flask - class UserRemap(db.Model): name = db.Column(db.String(40)) email = db.Column(db.String(255)) password = db.Column(db.String(64)) flag = db.Column(db.String(1)) def __init__(self, name, email, password): self.email = email self.name = name self.password = password self.flag='N' Here is table schema - mysql> desc UserRemap; +----------+--------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +----------+----

Using multiple POSTGRES databases and schemas with the same Flask-SQLAlchemy model

二次信任 提交于 2019-12-05 13:33:05
I'm going to be very specific here, because similar questions have been asked, but none of the solutions work for this problem. I'm working on a project that has four postgres databases, but let's say for the sake of simplicity there are 2. Namely, A & B A,B represent two geographical locations, but the tables and schemas in the database are identical. Sample model: from flask_sqlalchemy import SQLAlchemy from sqlalchemy import * from sqlalchemy.ext.declarative import declarative_base db = SQLAlchemy() Base = declarative_base() class FRARecord(Base): __tablename__ = 'tb_fra_credentials' recnr