flask-sqlalchemy

Flask-SQLAlchemy create_all()

本秂侑毒 提交于 2019-12-01 01:54:31
When i run the dbManager.create_all() command, it runs with out errors but fails to create the tables. When i delete the database and run the create_all() command, i get the no such database as ##### error which i should get but when the database does exist, nothing happens. Please can anyone see what i'm doing wrong? from blogconfig import dbManager class Art(dbManager.Model): id = dbManager.Column(dbManager.Integer, primary_key = True) title = dbManager.Column(dbManager.String(64), index = True, unique = True) content = dbManager.Column(dbManager.Text(5000)) def __repr__(self): return '<Art

how to store binary file recieved by Flask into postgres

半城伤御伤魂 提交于 2019-12-01 00:44:53
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 statement db.session.execute(""" INSERT INTO uploaded_file(id, name, type, data) VALUES (:id, :name, :type

PostgreSQL ILIKE query with SQLAlchemy

血红的双手。 提交于 2019-11-30 23:11:59
问题 I'd like to run a query that selects all posts, case insensitive, that have titles that match '%' + [some_phrase] + '%' . That is, select all rows that have titles that contain some phrase, case insensitive. From the research I've done, it looks like I need to use Postgres's ILIKE query for it to match case insensitive. How can I execute a query like this with SQLAlchemy? class Post(db.Model): id = db.Column(db.Integer, primary_key = True) title = db.Column(db.String(250)) content = db.Column

How to create relationship many to many in SQLAlchemy (python, flask) for model User to itself

你说的曾经没有我的故事 提交于 2019-11-30 22:21:15
I need to create a table called friends , it should looks like: friends: user_id friend_id I was trying to do this with tutorials from SQLALchemy, but I have not found how to make relation many-to-many for same table. Here's what I have tried: # friends table # many to many - user - user _friends = db.Table('friends', db.Column('user_id', db.Integer, db.ForeignKey('users.id')), db.Column('friend_id', db.Integer, db.ForeignKey('users.id')) ) class User(db.Model, UserMixin): # table name in database __tablename__ = 'users' # primary key for table in db id = db.Column(db.Integer, primary_key=True

Flask-SQLAlchemy create_all()

我只是一个虾纸丫 提交于 2019-11-30 21:00:50
问题 When i run the dbManager.create_all() command, it runs with out errors but fails to create the tables. When i delete the database and run the create_all() command, i get the no such database as ##### error which i should get but when the database does exist, nothing happens. Please can anyone see what i'm doing wrong? from blogconfig import dbManager class Art(dbManager.Model): id = dbManager.Column(dbManager.Integer, primary_key = True) title = dbManager.Column(dbManager.String(64), index =

Flask SQLAlchemy pagination error

核能气质少年 提交于 2019-11-30 20:12:52
I have this code and the all() method and every other method works on this and I have looked all over and I could that the method paginate() works on BaseQuery which is also Query @app.route('/') @app.route('/index') @app.route('/blog') @app.route('/index/<int:page>') def index(page = 1): posts = db.session.query(models.Post).paginate(page, RESULTS_PER_PAGE, False) return render_template('index.html', title="Home", posts=posts) but this gives me the error AttributeError: 'Query' object has no attribute 'paginate' I've looked everywhere and I can't find any solution to this. From your question.

sqlalchemy multiple foreign keys to same table

老子叫甜甜 提交于 2019-11-30 19:57:24
问题 I have a postgres database that looks something like this: Table "public.entities" Column | Type | Modifiers ---------------+-----------------------------+------------------------------------------------ id | bigint | not null default nextval('guid_seq'::regclass) type_id | smallint | not null name | character varying | Indexes: "entities_pkey" PRIMARY KEY, btree (id) Foreign-key constraints: "entities_parent_id_fkey" FOREIGN KEY (parent_id) REFERENCES entities(id) "entities_type_id_fkey"

Using SQLAlchemy models in and out of Flask

不想你离开。 提交于 2019-11-30 19:25:28
I'm trying to build SQLAlchemy models that can be used in Flask and in other non-Flask services. I know that in order to use these objects in Flask I can use the Flask-SQLAlchemy module and build the models like this: app_builder.py def create_app(config): # Create a Flask app from the passed in settings app = Flask('web_service') app.config.from_object(config) # Attach SQLAlchemy to the application from database import db db.init_app(app) database.py from flask_sqlalchemy import SQLAlchemy db = SQLAlchemy() class Job(db.Model): __tablename__ = 'job' job_id = db.Column(db.Integer, primary_key

One-to-many Flask | SQLAlchemy

↘锁芯ラ 提交于 2019-11-30 19:07:11
I am trying to create a one-to-many relationship using Flask and SQLAlchemy. I want the one-to-many relationship to be as so: "For any single movie, there can be multiple characters" Here it what I have so far, but it is saving in my DB as one-to-one right now. (One movie to one character, saving multiple times in DB for multiple characters) class Movie(db.Model): __tablename__ = "movies" id = db.Column('movies_id', db.Integer, primary_key=True) movie_type = db.Column('movie_type', db.Text()) def __init__(self, movie_type): self.movie_type = movie_type def __repr__(self): return '<Movie %r>' %

How do I seed a flask sql-alchemy database

99封情书 提交于 2019-11-30 18:46:11
I am new at python, I just learnt how to create an api using flask restless and flask sql-alchemy. I however would like to seed the database with random values. How do I achieve this? Please help. Here is the api code... import flask import flask.ext.sqlalchemy import flask.ext.restless import datetime DATABASE = 'sqlite:///tmp/test.db' #Create the Flask application and the FLask-SQLALchemy object app = flask.Flask(__name__) app.config ['DEBUG'] = True app.config['SQLALCHEMY_DATABASE_URI'] = DATABASE db = flask.ext.sqlalchemy.SQLAlchemy(app) #create Flask-SQLAlchemy models class TodoItem(db