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

Airflow initdb failed in Amazon Linux

I am trying to install Airflow in EC2 (Amazon Linux) and "airflow initdb" fails with the following error: $ AIRFLOW_HOME=/var/lib/airflow airflow initdb [2019-09-07 20:51:32,416] {__init__.py:51} INFO - Using executor SequentialExecutor Traceback…
kee
  • 10,969
  • 24
  • 107
  • 168
9
votes
4 answers

Alembic SQLAlchemy autogenerate MetaData error

I am trying to create a migration with alembic running alembic revision --autogenerate -m 'initial setup' but I'm getting the error: FAILED: Can't proceed with --autogenerate option; environment script /Users/paul/python/my_project/alembic/env.py…
paul41
  • 576
  • 7
  • 17
9
votes
1 answer

Construct the entire tree from a SQLAlchemy adjacency list relationship

I have a class Node with a self referential mapping 'children' (backref 'parent') representing a tree in SQLAlchemy and I want to select the entire tree. If I do session.query(Node).all() then every access to node.children triggers a select. If I…
SquaredLoss
  • 158
  • 5
9
votes
2 answers

Flask SQL Alchemy vs MyPy - error with Model type

I came across the following problem with the combination of flask_sqlalchemy and mypy. When I define a new ORM object like: class Foo(db.Model): pass where db is a database created using SQL Alchemy applied to flask app, mypy type check…
Marcin Możejko
  • 39,542
  • 10
  • 109
  • 120
9
votes
3 answers

sqlalchemy.orm.exc.UnmappedInstanceError: Class 'builtins.NoneType' is not mapped

I have 3 sqlalchemy models setup which are all one to many relationships. The User model contains many Task models and the Task model contains many Subtask models. When I execute the test_script.py I get the error…
Stephen Collins
  • 343
  • 1
  • 2
  • 10
9
votes
2 answers

Many to One relationship with SQLAlchemy in the same table

I have a table of 'Clients' where a client can be a child of another client. Here's the table definition. [ClientID] [int] IDENTITY(1,1) NOT NULL, [name] [varchar](50) NOT NULL, [VPFSID] [varchar](50) NOT NULL, [Type] [varchar](25) NULL, [ROHostID]…
Kurt Telep
  • 721
  • 1
  • 5
  • 9
9
votes
3 answers

How can i insert row into table sqlalchemy object?

i have table object that its name is tags and i don't know how to insert new row into it this is my table object : tags = db.Table('tags', db.Column('tag_id', db.Integer, db.ForeignKey('tag.id'), primary_key=True), db.Column('Post_id',…
user8424164
9
votes
1 answer

Work on multiple branches with Flask-Migrate

I'm using Flask-Migrate (Alembic) to manage SQLAlchemy database migrations. I'm working on two different branches with different migrations. If I switch branches, I get an error that the migration is not found. If i merge this branches into the…
Anna
  • 545
  • 5
  • 10
9
votes
4 answers

Pandas read_sql() - AttributeError: 'Engine' object has no attribute 'cursor'

I am trying to read data from MySQL query using pandas read_sql() method with python3+sqlalchemy+pymysql I tried to follow the following tutorials…
Adi
  • 407
  • 1
  • 4
  • 13
9
votes
2 answers

How to get Flask-SQLAlchemy to work with the Application Factory Pattern

I want to set up an sqlite database using Flask-SQLAlchemy. I am getting an Operational error (sqlite3.OperationalError) no such table. This is for a Web app written with flask. I want to interact with the database using Flask-SQLAlchemy extension.…
Zephers
  • 323
  • 1
  • 3
  • 8
9
votes
2 answers

How do I get PEP 484 type hints for flask_sqlalchemy classes?

How do I get PEP 484 type hints with the flask_sqlalchemy package? I see that there are type hints for SQLAlchemy provided by a third party, but is it possible to hook these up for models and queries defined with flask_sqlalchemy?
ldrg
  • 4,150
  • 4
  • 43
  • 52
9
votes
2 answers

Identifying sqlalchemy.exc.OperationalError

I'm trying to catch mysql/sqlalchemy OperationalErrors and replace handle access denied (1045) differently from connection refused (2003) sqlalchemy.exc.OperationalError: (pymysql.err.OperationalError) (1045, "Access denied for user … (Background on…
Gamification
  • 787
  • 5
  • 20
9
votes
1 answer

Global query timeout in MySQL 5.6

I need to apply a query timeout at a global level in my application. The query: SET SESSION max_execution_time=1 does this with MySQL 5.7. I am using MySQL 5.6 and cannot upgrade at the moment. Any solution with SQL Alchemy would also help.
Swati Sinha
  • 91
  • 1
  • 2
9
votes
2 answers

Column default value persisted to the table

I am currently using a Column that has the following signature: Column('my_column', DateTime, default=datetime.datetime.utcnow) I am trying to figure out how to change that in order to be able to do vanilla sql inserts (INSERT INTO ...) rather than…
hyperboreean
  • 8,273
  • 12
  • 61
  • 97
9
votes
1 answer

Mixing tornado and sqlalchemy

I'm trying to write a tornado web application that uses sqlalchemy in some request handlers. These handlers have two parts: one that takes a long time to complete, and another that uses sqlalchemy and is relatively fast. I would like to make the…
Miguel
  • 658
  • 1
  • 6
  • 19