flask-sqlalchemy

How can I get information from 2 separate tables with a single request in Flask?

折月煮酒 提交于 2021-02-08 11:26:30
问题 class posts(db.Model): __tablename__ = "posts" id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(100)) postName = db.Column(db.String(100)) postDescription = db.Column(db.String(500)) postLike = db.Column(db.Integer) class postComment(db.Model): __tablename__ = "postComment" id = db.Column(db.Integer, primary_key=True) postID = db.Column(db.Integer) senderName = db.Column(db.String(20)) commentPost = db.Column(db.String(300)) class PostsSchema(ma.Schema): class Meta:

How to use many-to-many fields in Flask view?

独自空忆成欢 提交于 2021-02-08 08:37:56
问题 I am trying to create a blog using Flask. Every post can have multiple tags also every tag could be associated with multiple posts. So I created a many-to-many relationship. My questions is how do i save multiple tags when creating a new post. And since every post can have different number of tags how do i show this is in the form? Also, how can i create new tags along with the post and then use those tags with other posts? This is models.py - postcategory = db.Table('tags', db.Column('posts

How to use many-to-many fields in Flask view?

亡梦爱人 提交于 2021-02-08 08:36:11
问题 I am trying to create a blog using Flask. Every post can have multiple tags also every tag could be associated with multiple posts. So I created a many-to-many relationship. My questions is how do i save multiple tags when creating a new post. And since every post can have different number of tags how do i show this is in the form? Also, how can i create new tags along with the post and then use those tags with other posts? This is models.py - postcategory = db.Table('tags', db.Column('posts

Accessing extra data in a sqlalchemy associated object

偶尔善良 提交于 2021-02-08 07:52:41
问题 The User object can be involved with a Project in many different ways, specified by Job association table. In my template file, I'm trying to loop through all of the projects in current_user.projects and with each one, specify what the Job was for that user in that project... but I can't get the syntax right. MODELS class Project(db.Model): id = db.Column(db.Integer(), primary_key=True) title = db.Column(db.String(80)) users = db.relationship("Job", back_populates="project") class User(db

Accessing extra data in a sqlalchemy associated object

a 夏天 提交于 2021-02-08 07:51:55
问题 The User object can be involved with a Project in many different ways, specified by Job association table. In my template file, I'm trying to loop through all of the projects in current_user.projects and with each one, specify what the Job was for that user in that project... but I can't get the syntax right. MODELS class Project(db.Model): id = db.Column(db.Integer(), primary_key=True) title = db.Column(db.String(80)) users = db.relationship("Job", back_populates="project") class User(db

Accessing extra data in a sqlalchemy associated object

牧云@^-^@ 提交于 2021-02-08 07:51:25
问题 The User object can be involved with a Project in many different ways, specified by Job association table. In my template file, I'm trying to loop through all of the projects in current_user.projects and with each one, specify what the Job was for that user in that project... but I can't get the syntax right. MODELS class Project(db.Model): id = db.Column(db.Integer(), primary_key=True) title = db.Column(db.String(80)) users = db.relationship("Job", back_populates="project") class User(db

SQL Alchemy, how to insert data into two tables and reference foreign key?

点点圈 提交于 2021-02-08 07:34:21
问题 I am inserting a list of python dictionaries into a Postgres database using SQL Alchemy (via Flask_sqlalchemy). One of the tables is a list of all unique items (table 1), while the second is a time series of data related to an item (table2). In essence, I want to insert any new row (with unique hash) in to table 1, then insert it's data to table 2. If it already exists in table 1, just insert the "child" in table 2 referencing the entry in table 1. This is one item in the list, the list has a

flask-sqlalchemy: Trouble joining tables from two databases (different bind keys). Getting error 1146 (see below)

孤人 提交于 2021-02-08 04:39:25
问题 I'm building a Flask-Restful API using python and sqlalchemy, and I'm trying to join two tables from different databases. It appears that I'm only able to search for tables in one database at a time. Am I missing something? from flask_sqlalchemy import SQLAlchemy from flask import Flask, jsonify, request app = Flask(__name__) app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://username:password@host:8000/database1' app.config['SQLALCHEMY_BINDS'] = { 'database2': 'mysql://username:password@host

Get the inserted primary key ids using bulk_save_objects

岁酱吖の 提交于 2021-02-07 21:54:45
问题 How can I get the inserted Ids (primary key generated) after using session.bulk_save_objects ? I tried this: for x in y: obj = Post(...) obj_list.append(obj) session.bulk_save_objects(obj_list) session.commit() for i in obj_list: print(i.id) The ids are None. The rows are successfully inserted. 回答1: you need to add return_defaults = True in bulk_save_object method like below to get primary key of records for x in y: obj = Post(...) obj_list.append(obj) session.bulk_save_objects(obj_list

SQLAlchemy: filtering count in many-to-many relationship query

大城市里の小女人 提交于 2021-02-07 10:20:35
问题 In my Flask app, there is a many-to-many relationship between Documents and Tokens: DocTokens = db.Table( 'DocTokens', db.Column('token_id', db.Integer, db.ForeignKey('Token.id')), db.Column('document_id', db.Integer, db.ForeignKey('Document.id')), ) class Token(db.Model): __tablename__ = 'Token' id = db.Column(db.Integer, primary_key=True) ... is_gold = db.Column(db.Boolean, default=None) class Document(db.Model): __tablename__ = 'Document' id = db.Column(db.Integer, primary_key=True) ...