flask-sqlalchemy

count unique rows in sql-alchemy

亡梦爱人 提交于 2020-01-03 05:45:08
问题 I am new to sql alchemy and I would like to count unique rows in my table and output the unique rows together with the number of copies for that row. Lets assume I have a table like this Table A -------------- id address now I want to get all rows of this table but for rows with the same adress I want to get only one row (doesn't matter which id). I also want to know how many ids are at a particular address. So if there are two people living at the same address "main street" (lets say id=4

'NoneType' object has no attribute 'owner' when trying to access relationship

独自空忆成欢 提交于 2020-01-03 05:26:13
问题 I'm trying to load a value on the Toy.owner relationshiop. These are my models: class User(db.Model): __tablename__ = 'User' id = db.Column(db.Integer, primary_key=True) type = db.Column(db.String(50)) nickname = db.Column(db.String(255)) email = db.Column(db.String(255)) password = db.Column(db.String(255)) __mapper_args__ = { "polymorphic_on":type, 'polymorphic_identity':'user', 'with_polymorphic':'*' } class Owner(User): __tablename__ = 'Owner' id = db.Column(db.Integer, db.ForeignKey(

Why is “Object of type Decimal is not JSON serializable” - when using marshmallow with SQLAlchemy automap?

社会主义新天地 提交于 2020-01-03 01:40:08
问题 Using automap_base from sqlalchemy.ext.automap to map my tables. Not able to shema.dumps(result) ; getting raise TypeError(f'Object of type {o.__class__.__name__} ' TypeError: Object of type Decimal is not JSON serializable Tried using JSON custom decoders, but no use. from flask import Flask from flask_sqlalchemy import SQLAlchemy from sqlalchemy.orm import Session from sqlalchemy.ext.automap import automap_base from flask_marshmallow import Marshmallow app = Flask(__name__) app.config[

Many-to-many multi-database join with Flask-SQLAlchemy

时光总嘲笑我的痴心妄想 提交于 2020-01-01 10:08:06
问题 I'm trying to make this many-to-many join work with Flask-SQLAlchemy and two MySQL databases, and it's very close except it's using the wrong database for the join table. Here's the basics... I've got main_db and vendor_db . The tables are setup as main_db.users , main_db.user_products (the relation table), and then vendor_db.products . Should be pretty clear how those are all connected. in my app.py, I'm seting up the databases like this: app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://user

Many-to-many multi-database join with Flask-SQLAlchemy

[亡魂溺海] 提交于 2020-01-01 10:08:06
问题 I'm trying to make this many-to-many join work with Flask-SQLAlchemy and two MySQL databases, and it's very close except it's using the wrong database for the join table. Here's the basics... I've got main_db and vendor_db . The tables are setup as main_db.users , main_db.user_products (the relation table), and then vendor_db.products . Should be pretty clear how those are all connected. in my app.py, I'm seting up the databases like this: app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://user

SQLAlchemy configuring many-to-many relationship to self using Association

徘徊边缘 提交于 2020-01-01 06:26:27
问题 I am having problems configuring a many to many relationship to a model itself. I can configure a self-many-to-many relation when I use anormal relationship configuration i.e. one not using an Association object. In this scenario I have to record some extra information in the many-to-many table itself so I'm trying to implement relationship using an Association object (PageLink). Here are the models. class PageLink(Base): ''' Association table. ''' __tablename__ = 'page_links' id = Column

flask-sqlalchemy use of drop_all and create_all for specific tables

若如初见. 提交于 2020-01-01 05:12:26
问题 In sqlalchemy (0.8.2), drop_all() and create_all() both have a tables parameter, which can be a list of Table objects to drop or add. In flask-sqlalchemy (1.0) these methods do not have this parameter. What is the appropriate way to drop / create a subset of database tables, using flask-alchemy? 回答1: Flask-SQLAlchemy's create_all() method will use the Base's metadata to create the table, by calling SQLAlchemy's MetaData.create_all() method. This method allows for a list of table objects to

How to define an unsigned integer in SQLAlchemy

余生颓废 提交于 2020-01-01 01:27:07
问题 I am migrating a portal to Flask with Flask-SQLAlchemy (MySQL). Below is the code I used to create my DB for my existing portal: Users = """CREATE TABLE Users( id INT UNSIGNED AUTO_INCREMENT NOT NULL, UserName VARCHAR(40) NOT NULL, FirstName VARCHAR(40) NOT NULL, LastName VARCHAR(40) NOT NULL, EmailAddress VARCHAR(255) NOT NULL, Password VARCHAR(40) NOT NULL, PRIMARY KEY (id) ) """ Here is how I am trying to use it in SQLAlchemy: class Users(db.Model): id = db.Column(db.Integer, primary_key

How can I override config parameters in Flask config?

感情迁移 提交于 2019-12-30 07:34:27
问题 I am using flask config and I configure my app like so: app = Flask(__name__) app.config.from_object('yourapplication.Config') My Config object is this: class Config(object): DEBUG = True TESTING = False DB_USER = os.getenv("DB_USER", "db_user") DB_PASSWORD = os.getenv("DB_PASSWORD", "db_password") DB_HOST = os.getenv("DB_HOST", "localhost") # 127.0.0.1" DB_PORT = os.getenv("DB_PORT", "5555") DB_SCHEMA = "my_schema" DB_DATABASE_NAME = "my_database" SQLALCHEMY_DATABASE_URI = "postgresql://{}:{

how to store binary file recieved by Flask into postgres

不羁的心 提交于 2019-12-30 07:17:24
问题 I currently have a Flask route that reveives file content via POST, and that stores it on the file system, ex: @app.route('/upload', methods=['POST']) def upload_file(): def allowed_file(f): return True file = request.files['file'] if file and allowed_file(file.filename): filename = secure_filename(file.filename) file.save(os.path.join(upload_dir(), filename)) return "", 200 I would like to store it in a BYTEA column in postgres, I am not sure how to bind the "data" argument to the insert