flask-sqlalchemy

display friends name by Flask Many-to-many sqlalchemy

泪湿孤枕 提交于 2019-12-13 04:47:31
问题 I have app that can make friends I am able to count friends and display it in profile.html But when I try to print the name of friends It doesn't work(It use flask-sqlalchemy) model.py: friends = db.Table('friends', db.Column('user_id', db.Integer, db.ForeignKey('user.id')), db.Column('friend_id', db.Integer, db.ForeignKey('user.id')) ) class User(db.Model): id = db.Column(db.Integer, primary_key = True) name = db.Column(db.String(50), index=True, unique= True) email = db.Column(db.String(50)

performing sparql query on the rdf data stored in relational database

我是研究僧i 提交于 2019-12-13 04:33:34
问题 I have stored large amount of RDF data into a relational database with the help of rdflib_sqlalchemy.SQLAlchemy.Now I want to execute Sparql query over the same.I can not find any thing to know how to implement sparql query here. Can anyone help. 回答1: Including sample code would make it easier to know what you are after but see the documentation on querying an RDFLib graph with SPARQL. Using the example from the rdflib-sqlalchemy README where graph is the name of the open graph. rq = "select

Build Model.query of a related model

懵懂的女人 提交于 2019-12-13 03:59:16
问题 I need to build a query that lists all the users, there best friend and there total number of friends. The list has to be ordered by totalFriends a user has. I want the resulting query to have the following structure: users.id | users.userName | users.userEmail | users.userPhone | totalFriends | bestFriends.userName | bestFriends.user_id Example: 1 | Alex | alex@alex.com | 900102030 | 2 | Carlos | 2 2 | Carlos | carlos@carlos.com | 900102030 | 1 | Alex | 1 3 | Sara | sara@sara.com | 900102030

Best association approach to connect Sensor model to others

无人久伴 提交于 2019-12-13 02:47:43
问题 I'm trying to implement the following with Flask+SQLAlchemy: I have two database models containing information about bee apiaries and bee hives. I would like to add feature to somehow connect both of them to Sensor model. Those can be attached to one of apiaries or one of bee hives. Here is what I have. class Apiary(db.Model): __tablename__ = 'apiary' # ... fields ... beehives = db.relationship("BeeHive", backref=db.backref('apiary', lazy='dynamic'), lazy='dynamic') class BeeHive(db.Model) _

Integer field not autoincrementing in SQLAlchemy

旧巷老猫 提交于 2019-12-13 02:12:11
问题 I have a Flask-SQLAlchemy model with an Integer field that I'd like to autoincrement. It's not a primary key; it's a surrogate ID. The model looks like: class StreetSegment(db.Model): id = db.Column(db.Integer, autoincrement=True) seg_id = db.Column(db.Integer, primary_key=True) When I create the table in my Postgres database, the id field is created as a plain integer. If I insert rows without specifying a value for id , it doesn't get populated. Is there some way I can force SQLAlchemy to

Flask-sqlalchemy column alias for browser output

拥有回忆 提交于 2019-12-13 00:55:06
问题 I'm using a for loop to output the columns and values of a single database row. This is all working but there are a couple of issues. The column names aren't suitable to output in the browser so I'm looking for a way to associate an alias (not sure this is the correct term) eg. column names: cust_name cust_area Desired output: Customer name Customer area models.py class Customers(db.Model): id = db.Column(db.Integer, primary_key = True) cust_name = db.Column(db.String(64)) cust_area = db

Count on related model as hybrid property

拜拜、爱过 提交于 2019-12-13 00:25:17
问题 I have a table called SectionAttempt which has many QuestionAttempts I want to display a computed field 'total_count' that is the number of question attempts for the SectionAttempt WITHOUT loading all the questionAttempts. Looking online , I found this: select([func.count()], QuestionAttempt.__table__.c.section_attempt_id==SectionAttempt.__table__.c.id) .correlate(SectionAttempt.__table__) .as_scalar() I am not 100% sure why this works, but it is working when I query like this: test_attempt =

SQLite Date type only accepts Python date objects as input

此生再无相见时 提交于 2019-12-13 00:18:43
问题 I have a problem with SQLAlchemy SQLite Date type only accepts Python date objects as input.[SQL: 'UPDATE records SET timestamp=?][parameters: [{'timestamp': (datetime.date(2017, 6, 15),)}]] This is my Forms.py class DataForm(FlaskForm): timestamp = DateField("date", validators=[Required()]) class ModifyDataForm(FlaskForm): timestamp = DateField("date", validators=[Required()]) This is my Models.py class Record(db.Model): timestamp = db.Column(db.Date, default=date.today, index=True) Views.py

Simple validation with SQLAlchemy

狂风中的少年 提交于 2019-12-12 21:15:43
问题 I'm new to sqlalchemy, and I'm trying to achieve simple validation of model's fields, as provided by Django ORM (min & max for Integer, email, ...). Can SQLAlchemy do this sort of field validations out of the box ? By the way, I'm using SQLAlchemy with Flask. 回答1: See Simple Validators in the documentation. Sample code extract below: class EmailAddress(Base): __tablename__ = 'address' id = Column(Integer, primary_key=True) email = Column(String) @validates('email') def validate_email(self,

PATCHing resources with nested objects with Flask/SQLAlchemy

感情迁移 提交于 2019-12-12 20:45:50
问题 I have the following setup: # models class Author(BaseModel): id = Column(Integer, primary_key=True) first_name = Column(String(64)) last_name = Column(String(64)) class Book(db.Model): id = Column(Integer, primary_key=True) title = Column(String(64)) author_id = Column(Integer, ForeignKey("author.id"), nullable=True) author = relationship(Author, backref=backref('books')) # schema class AuthorSchema(BaseSchema): first_name = fields.Str() last_name = fields.Str() class Meta(BaseSchema.Meta):