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
2 answers

flask "get_or_404" like function but with another status code

What I know: We all know that flask has a useful query.get_or_404 we can call it to any class object and return the object or raise a 404 error if the object is not found. The problem: I have a very large application and using that function for my…
Espoir Murhabazi
  • 5,973
  • 5
  • 42
  • 73
9
votes
3 answers

Is pytest supposed to collect tests from dependency modules in a virtual environtment?

I am attempting to setup a project on another laptop than my typical development machine. This project has several pytest-based tests that I have written over the lifetime of the project. When I run $ pytest -k tests/my_test.py I get a list of…
Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
9
votes
2 answers

Many-to-Many with three tables relating with each other (SqlAlchemy)

I've three tables User, Device and Role. I have created a many-to-many relation b/w User and Device like this; #Many-to-Many relation between User and Devices userDevices = db.Table("user_devices", db.Column("id", db.Integer,…
Anum Sheraz
  • 2,383
  • 1
  • 29
  • 54
9
votes
3 answers

Delete parent if the child is deleted

I want to delete the parent row if the associated rows in child tables have been removed. class Child(Base): __tablename__ = "children" id = Column(Integer, primary_key=True) parent_id = Column(Integer, ForeignKey("parents.id",…
mad_
  • 8,121
  • 2
  • 25
  • 40
9
votes
2 answers

Is connection pool in sqlalchemy thread-safe?

Documentation says that connection pool also is not designed for multithreading: It’s critical that when using a connection pool, and by extension when using an Engine created via create_engine(), that the pooled connections are not shared to a…
Nikita Ryanov
  • 1,520
  • 3
  • 17
  • 34
9
votes
1 answer

Using proper file structure with SQLAlchemy and how to add data to db

I am trying to build a simple blogging platform to learn Python and Flask. I am using SQLAlchemy to connect to a Postgres db hosted on Heroku and flask_s3 to serve static files from an AWS bucket. Im mostly following along from this:…
ron_kuby
  • 111
  • 1
  • 6
9
votes
2 answers

Sqlalchemy query returns Decimal object

I have the following model: 172 class ReportRecord(db.Model): 173 …
Ishan Khare
  • 1,745
  • 4
  • 28
  • 59
9
votes
1 answer

Multiple conditions in postgresql_where?

postgresql_where is useful to get around the (in my opinion wrong, but apparently the SQL standard defines it) way in which Postgres defines unique-ness, where Null values are always unique. A typical example is shown below, where no item can have…
Ng Oon-Ee
  • 1,193
  • 1
  • 10
  • 26
9
votes
1 answer

Avoid Parameter Binding When Executing Query with SQLAlchemy

I am using SQLALchemy to execute queries on Teradata. One of the queries I execute is a DDL statement to replace a stored procedure: REPLACE PROCEDURE DEV_MIGRATION_TOOL.UNIT_TEST_NEW_STORED_PROCEDURE() UNIT_TEST_NEW_STORED_PROCEDURE: BEGIN …
Alexis.Rolland
  • 5,724
  • 6
  • 50
  • 77
9
votes
1 answer

Why does this query give different results depending on how I arrange my DateTime arithmetic?

I have used SqlAlchemy to create a table, Record. Each record has a field, date, which stores a DateTime. I want to find all records whose date is more recent than eight hours ago. I came up with four ways to write a filter, all involving simple…
Kevin
  • 74,910
  • 12
  • 133
  • 166
9
votes
4 answers

When inheriting SQLAlchemy class from abstract class exception thrown: metaclass conflict: the metaclass of a derived class must be

The following code is a very simple implementation of a SqlAlchemy ORM with one simple table. The Mytable class tries to inherit from BaseAbstract. The code throws the following exception: Message: metaclass conflict: the metaclass of a derived…
Barka
  • 8,764
  • 15
  • 64
  • 91
9
votes
1 answer

SQLAlchemy encrypt a column without automatically decrypting upon retrieval

Currently, I use EncryptedType from sqlalchemy_utils in order to automatically encrypt data on the way in to the table and decrypt the data when it's being retrieved from the table, using some predefined string as the encryption key. This works…
Jfach
  • 301
  • 1
  • 3
  • 11
9
votes
2 answers

flask-migrate cannot drop table because other objects depend on it

Problem applying just created migration (Added db.Model) through Flask-Migrate (SQLAlchemy) for PostgresSQL DB. The error itself: sqlalchemy.exc.InternalError: (psycopg2.InternalError) cannot drop table parameter_subtype because other objects depend…
garmoncheg
  • 867
  • 11
  • 26
9
votes
1 answer

Is SQL injection protection built into SQLAlchemy's ORM or Core?

I'm developing an aiohttp server application, and I just saw that apparently it isn't able to use SQLAlchemy's ORM layer. So, I was wondering: if my application will only be able to use SQLAlchemy's core, is it still protected against SQL injection…
Sander Vanden Hautte
  • 2,138
  • 3
  • 22
  • 36
9
votes
1 answer

Declarative SQLAlchemy CREATE SQLITE in memory tables

This is how I setup my database for an application (in Flask): from sqlalchemy.engine import create_engine from sqlalchemy.orm import scoped_session, create_session from sqlalchemy.ext.declarative import declarative_base engine = None db_session =…
Radek
  • 3,913
  • 3
  • 42
  • 37