flask-sqlalchemy

SQLALCHEMY_DATABASE_URI not set

我的未来我决定 提交于 2019-11-30 18:03:24
I tried to work with CURD operation using Flask and SQLAlchemy .But getting Error while connecting to database. Here is the Error log. /usr/local/lib/python3.4/dist-packages/flask_sqlalchemy/__init__.py:819: UserWarning: SQLALCHEMY_DATABASE_URI not set. Defaulting to "sqlite:///:memory:". 'SQLALCHEMY_DATABASE_URI not set. Defaulting to ' /usr/local/lib/python3.4/dist-packages/flask_sqlalchemy/__init__.py:839: FSADeprecationWarning: SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will be disabled by default in the future. Set it to True or False to suppress this warning.

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

China☆狼群 提交于 2019-11-30 17:52:44
问题 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

How to use marshmallow to serialize a custom sqlalchemy field?

无人久伴 提交于 2019-11-30 17:32:18
问题 I just start a simple project called flask_wiki this days and I'm using some flask extensions as the follows: Flask-SQLAlchemy Flask-Restful MarshMallow Well, I just discovered that the MarshMallow project provides a class called 'ModelSchema', which reads all fields from my SQLAlchemy Model and provide a fully automated (de)serialializer. In my case, I created a 'GUID' Field which is RDBM agnostic and inserted it on my Sqlalchemy model as follows: from flask.ext.sqlalchemy import SQLAlchemy

SQLAlchemy order by hybrid property that references relationship

时光总嘲笑我的痴心妄想 提交于 2019-11-30 15:48:49
My SQLAlchemy models: class Cover(db.Model): # ... a bunch of other fields ... @hybrid_property def number_of_requests(self): if self.requests: return len(self.requests) return 0 @number_of_requests.expression def number_of_requests(cls): return func.count(cls.requests) class Request(db.Model): # ... a bunch of other fields ... # Cover that this request is requesting cover_id = db.Column(db.Integer, db.ForeignKey('cover.id') cover = db.relationship('Cover', backref=backref("requests", cascade="all, delete-orphan")) So, a simple one-to-many relationship between Cover and Request. The number_of

Structure Flask-Restful API to use SQLAlchemy

假装没事ソ 提交于 2019-11-30 15:46:33
So I'm trying to make an API, using Flask-Restful, but all the examples I find put everything into one app.py file. I found information in the Flask-Restful docs explaining how to structure your API, but it doesn't include anything for using a database. I've posted what I've come up with, and it works if I hard-code some data, but when I import the db into users.py I get an error ImportError: cannot import name 'db' . So, what is the best way to structure an API to bring in your data from a database? Structure myapi run.py api __init__.py resources __init__.py user.py myapi/run.py from api

Dependency rule tried to blank-out primary key column in SQL-Alchemy when trying to delete record

夙愿已清 提交于 2019-11-30 15:34:58
I have a many to one relationship between the Userownedshare table and the Share table. When I delete a Userownedshare entry from the database I get the following error: AssertionError: Dependency rule tried to blank-out primary key column 'share.ticker' This makes sense as the ticker field in Userownedshare is a Foreign Key in the Share table. However I cannot work out how to fix this error. I think that I want to set up a cascading deletion when a Share entry is orphaned but I cannot work out how to do this, I have read the documentation but I just end up with different types of errors so I

SQLAlchemy - Querying with DateTime columns to filter by month/day/year

淺唱寂寞╮ 提交于 2019-11-30 14:20:33
I'm building a Flask website that involves keeping track of payments, and I've run into an issue where I can't really seem to filter one of my db models by date. For instance, if this is what my table looks like: payment_to, amount, due_date (a DateTime object) company A, 3000, 7-20-2018 comapny B, 3000, 7-21-2018 company C, 3000, 8-20-2018 and I want to filter it so that I get all rows that's after July 20th, or all rows that are in August, etc. I can think of a crude, brute-force way to filter all payments and THEN iterate through the list to filter by month/year, but I'd rather stay away

SQLAlchemy: Hybrid expression with relationship

社会主义新天地 提交于 2019-11-30 13:38:19
问题 I have two Flask-SQLAlchemy models with a simple one-to-many relationship, like the minimal example below: class School(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(30)) address = db.Column(db.String(30)) class Teacher(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(30)) id_school = db.Column(db.Integer, db.ForeignKey(School.id)) school = relationship('School', backref='teachers') Then I add an hybrid property to teacher

SQLAlchemy: Hybrid expression with relationship

主宰稳场 提交于 2019-11-30 10:52:07
I have two Flask-SQLAlchemy models with a simple one-to-many relationship, like the minimal example below: class School(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(30)) address = db.Column(db.String(30)) class Teacher(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(30)) id_school = db.Column(db.Integer, db.ForeignKey(School.id)) school = relationship('School', backref='teachers') Then I add an hybrid property to teacher that uses the relationship, like: @hybrid_property def school_name(self): return self.school.name And

Could not assemble any primary key columns for mapped table

半城伤御伤魂 提交于 2019-11-30 10:48:18
When I'm trying to create a database schema migration, I'm getting this weird error. Can you please help me to figure out what's wrong? $ python app.py db upgrade [skipped] sqlalchemy.exc.ArgumentError: Mapper Mapper|EssayStateAssociations|essay_associations could not assemble any primary key columns for mapped table 'essay_associations' My model: class EssayStateAssociations(db.Model): __tablename__ = 'essay_associations' application_essay_id = db.Column( db.Integer, db.ForeignKey("application_essay.id"), primary_key=True), theme_essay_id = db.Column( db.Integer, db.ForeignKey("theme_essay.id