Join the Stack Overflow Community
Stack Overflow is a community of 6.5 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I started creating a flask application as I will be needing to do some visualization of some data that was recently transferred to a local postgresql database.

I have a lot of tables (33) which all have the same kind of content as currently just works as containers for different types of data (They contain a JSON object that's different in each of the tables though).

The tables are named "Table_#" (where # symbolizes a number among a defined subset of numbers).

models.py

I have created the following model:

from sqlalchemy import BigInteger, Column, JSON, Text
from app import db

class Table_1(db.Model):
    __tablename__ = 'table_1'

    id = db.Column(BigInteger, primary_key=True)
    did = db.Column(Text)
    timestamp = db.Column(BigInteger)
    data = db.Column(JSON)
    db_timestamp = db.Column(BigInteger)

    def __repr__(self):
        return '<1_Data %r>' % (self.did)

to test if I can actually grab some data from my postgresql.

__init__.py

from flask import Flask
from flask_sqlalchemy import SQLAlchemy

app = Flask(__name__)
app.config.from_object('config')
db = SQLAlchemy(app)

from app import views, models

config.py

In my config.py I have the following:

SQLALCHEMY_DATABASE_URI = "postgresql://<username>:<password>@localhost:5432/backupdata" //Ofc <username> and <password> isn't my credentials..

In my terminal (Windows cmd) I've tried running the following:

>python
>>>from app import db, models
>>>row = models.Table_1.query.get(1)

But for some reason when I try to print my row nothing is output.

I was hoping someone could have an idea why that is?

share|improve this question
    
Do you have a record in the table with a primary key of 1? – dirn Dec 5 at 12:32
    
Does the above command get the id's of the rows? I thought it was like just get 1 row.. But if it's the ID of the row then that is my problem. How would I got about getting a "random" row or a random subset of rows? – Zeliax Dec 5 at 14:42
1  
get does a primary key lookup. – dirn Dec 5 at 14:44
    
Then I am definitively using the wrong command. Thanks for informing me. I will get right on checking the documentation ;) – Zeliax Dec 5 at 15:03
up vote 0 down vote accepted

I updated my model a bit, to see that I actually got a specific row.

from sqlalchemy import BigInteger, Column, JSON, Text
from app import db

class Table_1(db.Model):
    __tablename__ = 'table_1'

    id = db.Column(BigInteger, primary_key=True)
    did = db.Column(Text)
    timestamp = db.Column(BigInteger)
    data = db.Column(JSON)
    db_timestamp = db.Column(BigInteger)

    def __repr__(self):
        return '<1_Data %r, %r>' % (self.did, self.id)

And then the following query made me aware that actually have a working connection to SQLAlchemy through my flask application.

row = models.Table_910.query.filter_by(did='357139052424715').first()

Knowing that I have one item with the above did I could get a single item:

<1_Data '357139052424715', 738390911>
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.