Questions tagged [sqlalchemy]

SQLAlchemy is a Python SQL toolkit and Object Relational Mapper that gives application developers the full power and flexibility of SQL.

SQLAlchemy provides a full suite of well known enterprise-level persistence patterns, designed for efficient and high-performing database access, adapted into a simple and Pythonic domain language.

NOTE: PLEASE report bugs / advanced issues as a GitHub discussion or on the sqlalchemy mailing list - much more comprehensive help for complex issues is available there. Please follow these guidelines when posting.

Example

metadata = MetaData()
engine = sqlalchemy.create_engine('mysql://user:pass@localhost/sql_db') # session handler

names = Table('names', metadata,
  Column('id', Integer),
  Column('name', String(60))
)

metadata.create_all(engine) # create tables in case they do not exist

# ask user for id and name
_id = int(input('indicate id: '))  # use `_id`, as `id` is a reserved word in python
name = input('indicate name: ')

ins = names.insert().values(name=name, id=_id) # insert values in table

References

Books:

Talks:

22992 questions
9
votes
1 answer

sqlalchemy: select specific columns from multiple join using aliases

This has be stumped for more than a day now and examples I could find have not worked. I am new to SQLALCHEMY and I find the documentation not very enlightening. The query (so far): prey = alias(ensembl_genes, name='prey') bait =…
John van Dam
  • 393
  • 2
  • 9
9
votes
0 answers

SQLAlchemy filtering nested JSON data in PostgreSQL JSONB

I'm storing Nesting JSON down as jsonb, but I have no idea how to select nested json with an uncertain value. e.g.: { "facebook": { "openid": "123456789", "access_token": "6EFD26B0A868E3BB387E78851E42943F" } } I know the…
Nightsuki
  • 91
  • 1
  • 6
9
votes
2 answers

Renaming columns when querying with SQLAlchemy into Pandas DataFrame

Is there a way to retain the SqlAlchemy attribute names when you query the data into a pandas dataframe? Here's a simple mapping of my database. For the school table, I've renamed 'SchoolDistrict', the DB name, to a shorter 'district'. I'm several…
AZhao
  • 13,617
  • 7
  • 31
  • 54
9
votes
2 answers

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

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…
Alex Pana
  • 253
  • 2
  • 10
9
votes
1 answer

SAWarning when querying with SQLAlchemy into pandas df

I'm querying my SQLAlchemy-mapped star schema directly into a pandas DataFrame and am getting an annoying SAWarning from pandas that I'd like to address. Here's a simplified version. class School(Base): __tablename__ = 'DimSchool' id =…
AZhao
  • 13,617
  • 7
  • 31
  • 54
9
votes
1 answer

Can I add a new item based on another database row in SQLAlchemy?

As the title suggests, I want to add a row with an id based on another row in SQLALchemy. Currently, I'm adding an extra select, but I'd like to get rid of this. I know this is possible in MySQL, so I'm just trying to figure out the SQLAlchemy…
Eli
  • 36,793
  • 40
  • 144
  • 207
9
votes
2 answers

Handle mysql restart in SQLAlchemy

My Pylons app uses local MySQL server via SQLAlchemy and python-MySQLdb. When the server is restarted, open pooled connections are apparently closed, but the application doesn't know about this and apparently when it tries to use such connection it…
wRAR
  • 25,009
  • 4
  • 84
  • 97
9
votes
1 answer

SQLAlchemy "or" statement with multiple parameters

I have a query that require to use the "or" | operator : Mymodel.query.filter((Mymodel.a== 'b') | (Mymodel.b == 'c')) That works fine. However, I want my conditions to be put in an array of unkown length : conds = [ Mymodel.a== 'b', Mymodel.b ==…
Blusky
  • 3,470
  • 1
  • 19
  • 35
9
votes
3 answers

Creating class instance from dictionary?

I am trying to create class instance from dictionary that has keys more than class has attributes. I already read answers on the same question from this link: Creating class instance properties from a dictionary?. The problem is that I can't write…
Demyanov
  • 901
  • 2
  • 10
  • 15
9
votes
1 answer

How to delete a one-to-one relationship with SQLAlchemy

I would like to create a nullable, self-referencing relationship which can be deleted using SQLAlchemy. An example model is as follows (note, using Flask-SQLAlchemy): class Person(db.Model): __tablename__ = 'person' id =…
bbengfort
  • 5,254
  • 4
  • 44
  • 57
9
votes
2 answers

Help with copy and deepcopy in Python

I think I tried to ask for far too much in my previous question so apologies for that. Let me lay out my situation in as simple a manner as I can this time. Basically, I've got a bunch of dictionaries that reference my objects, which are in turn…
PizzAzzra
  • 715
  • 13
  • 25
9
votes
2 answers

Flask-Sqlalchemy + Sqlalchemy-searchable returning empty list

First time on the site, so hi to all and thanks in advance. Longtime lurker and newb. I'm working on a web app in flask, using Flask-SqlAlchemy and SqlAlchemy-Searchable (docs-> https://sqlalchemy-searchable.readthedocs.org/en/latest/index.html).…
Vyndion
  • 151
  • 8
9
votes
3 answers

sqlalchemy.exc.ResourceClosedError: This Connection is closed when inserting after select

I am doing a select() from a SQLite database and then an insert(): engine = create_engine('sqlite:///testdb.db') metadata = MetaData(bind=engine) test = Table('test', metadata, autoload=True) # Select all from pending_data sel = select([test]) res…
mchangun
  • 9,814
  • 18
  • 71
  • 101
9
votes
2 answers

ImportError: No module named 'pysqlite2' when running tests in Python 3 Ubuntu

Some background: we have a codebase written in Python 3 that uses Pyramid and the SqlAlchemy ORM to persist to a mysql database. To write tests for our classes using the ORM we are using Sqlite. All of this works fine together... locally. Setting up…
Nathaniel Ford
  • 20,545
  • 20
  • 91
  • 102
9
votes
3 answers

sqlalchemy filter by comparing datetime.now() and column default date

in my table definition, i have a column defined like this: created_date = Column(DateTime, nullable=False, default=datetime.now) i want to query a instance when its created date is equal to current date(eg, if it is created today). so i tried…
shangsunset
  • 1,585
  • 4
  • 22
  • 38
1 2 3
99
100