flask-sqlalchemy

Flask-Migrate No Changes Detected to Schema on first migration

别说谁变了你拦得住时间么 提交于 2020-05-25 05:00:05
问题 I'm using Flask with Flask-SQLAlchemy and Flask-Migrate to create an application, however when I try to create a migration nothing happens. I've created two tables in app/models.py : from flask import current_app from . import db class Student(db.Model): __tablename__ = 'students' id = db.Column(db.Integer, primary_key=True) email = db.Column(db.String(64), unique=True, nullable=False) password_hash = db.Column(db.String(128)) def __init__(self, **kwargs): super(Student, self).__init__(*

SQL Alchemy: Is there a way to get ID of parent before committing?

吃可爱长大的小学妹 提交于 2020-05-17 06:26:07
问题 is there a way to somehow get the ID of Parent before committing? I have a db that has 2 tables. Experiment and ExperimentRule. See my models: class Experiment(db.Model): __tablename__ = 'TestE' __table_args__ = {'extend_existing': True} ExperimentID = db.Column(db.Integer, primary_key = True) ExperimentName = db.Column(db.String(255), nullable = False) Status = db.Column(db.String(50), nullable = False) Description = db.Column(db.String(1000), nullable = False) URL = db.Column(db.String(255)

How do I express “WHERE 'value' like column” in SQLAlchemy?

让人想犯罪 __ 提交于 2020-05-16 11:24:26
问题 I'm trying to do a where clause in reverse order with SQLAlchemy ORM. So instead of Table.query.filter(Table.column.like(value)) , I'd like to end up with... select * from table where 'mail.google.com' like domain; ...to select this row: | domain | | ------------ | | %.google.com | Ideally, I'd be able to do this: Table.query.filter(BinaryExpression('mail.google.com', Table.domain, custom_op('like')).all() But it return the AttributeError: 'str' object has no attribute 'self_group'. How is

SQLAlchemy: How to add column to existing table?

家住魔仙堡 提交于 2020-05-15 10:18:46
问题 I am trying to add a field or column to an existing table using SQLAchemy. Below is the table class class ReleaseVersion(Base): __tablename__ = 'versions' id = Column(Integer, primary_key=True, autoincrement=True) release = Column(String(128), nullable=False, unique=True) def __init__(self,release, id=None): if(id): self.id = id self.release = release I initialized the table using following line myDB.ReleaseVersion.__table__.create(bind=self.engine, checkfirst=True) After using the DB for

SQLAlchemy: How to add column to existing table?

本小妞迷上赌 提交于 2020-05-15 10:18:30
问题 I am trying to add a field or column to an existing table using SQLAchemy. Below is the table class class ReleaseVersion(Base): __tablename__ = 'versions' id = Column(Integer, primary_key=True, autoincrement=True) release = Column(String(128), nullable=False, unique=True) def __init__(self,release, id=None): if(id): self.id = id self.release = release I initialized the table using following line myDB.ReleaseVersion.__table__.create(bind=self.engine, checkfirst=True) After using the DB for

Flask-SQLAlchemy– Can you make a query within a model?

六眼飞鱼酱① 提交于 2020-05-13 18:06:23
问题 I'm building a flask webapp which uses Flask-SQLAlchemy, and I'm also considering using Flask-Login to take care of sessions and to protect certain views. Flask-Login requires certain methods which I see as useful for various parts of the app (specifically, is_authenticated() and is_active() . However, in all of the examples I've seen these methods just return something fixed. What if I want to make a query on the database. For example, if I want to check if that user actually has an entry in

Flask-sqlalchemy / uwsgi: DB connection problem when more than on process is used

我怕爱的太早我们不能终老 提交于 2020-04-30 11:16:05
问题 I have a Flask app running on Heroku with uwsgi server in which each user connects to his own database. I have implemented the solution reported here for a very similar situation. In particular, I have implemented the connection registry as follows: class DBSessionRegistry(): _registry = {} def get(self, URI, **kwargs): if URI not in self._registry: current_app.logger.info(f'INFO - CREATING A NEW CONNECTION') try: engine = create_engine(URI, echo=False, pool_size=5, max_overflow=5) session

How to connect docker volume 'database/app.db' to SQLAlchemy?

a 夏天 提交于 2020-04-18 05:46:29
问题 I am using Python 3.8.1 Flask latest and app running fine but unable to connect the docker volume from SQLAlchemy and if the database does not exist it should create the database, but I am unable to connect my shared docker volume into my Python Flask app. Config.py file: import os basedir = os.path.abspath(os.path.dirname(__file__)) DATA_PATH = "/database" if os.path.exists(DATA_PATH) == False: os.mkdir(DATA_PATH) class Config(object): # set a proper secret key here or is the .flaskenv file

How do I tell an ElasticSearch multi-match query that I want numeric fields, stored as strings, to return matches with numeric strings?

好久不见. 提交于 2020-04-18 03:49:34
问题 I am writing a Flask app and I am using elasticsearch. Here is search.py : from flask import current_app def query_object(index, fields, query, page, per_page, fuzziness=0): search = current_app.elasticsearch.search( index=index, body={'query': {'multi_match': {'query': str(query), 'fields': fields, 'fuzziness': fuzziness, 'lenient': True}}, 'from': (page - 1) * per_page, 'size': per_page} ) ids = [int(hit['_id']) for hit in search['hits']['hits']] return ids, search['hits']['total']['value']

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

别说谁变了你拦得住时间么 提交于 2020-04-15 05:28:12
问题 I want to evaluate the values that are linked to two different users. I created a different table to keep track of a user's previous experiences and their interested experiences and linked them to the user. I have a matching function that tries to find the user with the most similarities, but it gives the attribute error listed in the title. model: class User(UserMixin, db.Model): id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(64), index=True, unique=True) prev =