flask-sqlalchemy

No changes detected in Alembic autogeneration of migrations with Flask-SQLAlchemy

巧了我就是萌 提交于 2019-11-30 05:01:20
I'm having trouble getting Alembic to autogenerate candidate migrations from changes to classes using db.Model (Flask-SQLAlchemy) instead of Base . I've modified env.py to create my Flask app, import all relevant models, initialize the database, and then run migrations: ... uri = 'mysql://user:password@host/dbname?charset=utf8' app = Flask(__name__) app.config['SQLALCHEMY_DATABASE_URI'] = uri app.config['SQLALCHEMY_ECHO'] = True db.init_app(app) with app.test_request_context(): target_metadata = db.Model.metadata config.set_main_option('sqlalchemy.url', uri) if context.is_offline_mode(): run

How to add comments to table or column in mysql using SQLAlchemy?

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-30 04:43:52
问题 I want to add comments a tabble and columns created. so I add doc parameter to the constructor of SQLAlchemy Column class. But do not add comments the column. class Notice(db.Model): __tablename__ = "tb_notice" __table_args__ = {'mysql_engine': 'MyISAM'} seqno = db.Column(db.Integer, primary_key=True, autoincrement=True, doc="seqno") title = db.Column(db.String(200), nullable=False, doc="notice title") detail = db.Column(db.TEXT, nullable=True, doc="notice detail ") And I wonder how to add

One-to-many Flask | SQLAlchemy

橙三吉。 提交于 2019-11-30 03:38:35
问题 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())

Using SQLAlchemy models in and out of Flask

感情迁移 提交于 2019-11-30 03:26:21
问题 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

How do I seed a flask sql-alchemy database

这一生的挚爱 提交于 2019-11-30 03:03:25
问题 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'] =

How does Flask-SQLAlchemy create_all discover the models to create?

北城以北 提交于 2019-11-30 02:26:44
Flask-SQLAlchemy's db.create_all() method creates each table corresponding to my defined models. I never instantiate or register instances of the models. They're just class definitions that inherit from db.Model . How does it know which models I have defined? Flask-SQLAlchemy does nothing special, it's all a standard part of SQLAlchemy. Calling db.create_all eventually calls db.Model.metadata.create_all . Tables are associated with a MetaData instance as they are defined . The exact mechanism is very circuitous within SQLAlchemy, as there is a lot of behind the scenes bookkeeping going on, so

SQLAlchemy order by hybrid property that references relationship

大憨熊 提交于 2019-11-29 23:18:02
问题 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

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

浪子不回头ぞ 提交于 2019-11-29 20:48:57
问题 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

sqlalchemy foreign key relationship attributes

喜欢而已 提交于 2019-11-29 20:18:13
I have a User table and a Friend table. The Friend table holds two foreign keys both to my User table as well as a status field. I am trying to be able to call attributes from my User table on a Friend object. For example, I would love to be able to do something like, friend.name, or friend.email. class User(Base): """ Holds user info """ __tablename__ = 'user' id = Column(Integer, primary_key=True) name = Column(String(25), unique=True) email = Column(String(50), unique=True) password = Column(String(25)) admin = Column(Boolean) # relationships friends = relationship('Friend', backref='Friend

Creating seed data in a flask-migrate or alembic migration

╄→尐↘猪︶ㄣ 提交于 2019-11-29 19:26:17
How can I insert some seed data in my first migration? If the migration is not the best place for this, then what is the best practice? """empty message Revision ID: 384cfaaaa0be Revises: None Create Date: 2013-10-11 16:36:34.696069 """ # revision identifiers, used by Alembic. revision = '384cfaaaa0be' down_revision = None from alembic import op import sqlalchemy as sa def upgrade(): ### commands auto generated by Alembic - please adjust! ### op.create_table('list_type', sa.Column('id', sa.Integer(), nullable=False), sa.Column('name', sa.String(length=80), nullable=False), sa