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

How to use joinedload/contains_eager for query-enabled relationships (lazy='dynamic' option) in SQLAlchemy

I have the following model classes declared by SQLAlchemy: class User(Base): id = Column(Integer, primary_key=True) name = Column(String, nullable=False, unique=True) created_at = Colmn(DateTime, nullable=False,…
minhee
  • 5,688
  • 5
  • 43
  • 81
9
votes
2 answers

How to find sqlalchemy remote side object's class or class name without db queries?

Let's have a classes X and Y and relations between them x2y and y2x. From class_mapper(Class).iterate_properties iterator we can get all class's properties. So x2y and y2x are RelationshipProperty and what I hope to get from is a class or a class…
mdob
  • 2,224
  • 3
  • 22
  • 25
9
votes
1 answer

Custom json serializer for JSON column in SQLAlchemy

I have following ORM object (simplified): import datetime as dt from sqlalchemy import create_engine, Integer, Column, DateTime from sqlalchemy.dialects.postgresql import JSONB from sqlalchemy.orm import Session, declarative_base Base =…
Eduard Sukharev
  • 1,188
  • 2
  • 15
  • 37
9
votes
1 answer

SQLAlchemy 2.0 NotImplementedError: engine.execute

I am getting a NotImplementedError: This method is not implemented for SQLAlchemy 2.0., when trying to delete a table using the delete method in SQLAlchemy v1.4.15. from sqlalchemy import Column, Integer, MetaData, String, Table, create_engine,…
Andi
  • 3,196
  • 2
  • 24
  • 44
9
votes
4 answers

Escaping special characters in filepaths using SQLAlchemy

I'm storing filenames and filepaths in MySQL. Retrieving them from the database using LIKE expressions requires that I escape all allowed filename chars that collide with MySQL special chars. I'm happy to simply use Python's string.replace()…
Paul
  • 2,973
  • 6
  • 31
  • 40
9
votes
1 answer

When to use `session_maker` and when to use `Session` in sqlalchemy

Sqlalchemy's documentation says that one can create a session in two ways: from sqlalchemy.orm import Session session = Session(engine) or with a sessionmaker from sqlalchemy.orm import session_maker Session = session_maker(engine) session =…
Felix B.
  • 905
  • 9
  • 23
9
votes
4 answers

Sqlalchemy: Latitude and Longitude Float Precision?

I'm using Sqlalchemy to define my tables and such and here is some code I came up with: locations = Table('locations', Base.metadata, Column("lat", Float(Precision=64), primary_key=True), Column("lng", Float(Precision=64), primary_key=True), ) I…
john
  • 3,043
  • 5
  • 27
  • 48
9
votes
1 answer

how to do a join in sqlalchemy session query?

I need to find the equivalent of this query in sqlalchemy. SELECT u.user_id, u.user_name, c.country FROM table_user u , table_country c WHERE u.user_email = 'abc@def.com' i tried this below…
skoovill
  • 1,069
  • 2
  • 12
  • 22
9
votes
1 answer

SQLAlchemy | Multiple One-To-One and One-To-Many relationships

I've got two models: Game and Player. I want to have a list of all players in the Game Model, as well as one of these players in a different field. It's a flask server and a sqlite database. Here is my Player Model: class Player(db.Model): id =…
9
votes
3 answers

SQLAlchemy accessing column types from query results

I am connecting to a SQL Server database using SQLAlchemy (with the pymssql driver). import sqlalchemy conn_string = f'mssql+pymssql://{uid}:{pwd}@{instance}/?database={db};charset=utf8' sql = 'SELECT * FROM FAKETABLE;' engine =…
Sumedh
  • 4,835
  • 2
  • 17
  • 32
9
votes
2 answers

SQLAlchemy : how can I execute a raw INSERT sql query in a Postgres database?

I’m building an app using Python and the clean architecture principles, with TDD. Some unit tests require executing some raw SQL queries against an in-memory database. I am trying to switch from sqlite to postgresql inmemory data, using…
Lionel Hamayon
  • 1,240
  • 15
  • 25
9
votes
2 answers

List of object attributes in pydantic model

I use Fast API to create a web service. There are following sqlAlchemy models: class User(Base): __tablename__ = 'user' account_name = Column(String, primary_key=True, index=True, unique=True) email = Column(String, unique=True,…
Dmitry Ezhov
  • 315
  • 2
  • 3
  • 10
9
votes
3 answers

SQLAlchemy error: "TypeError: Additional arguments should be named _, got 'nullable'"

Problem I get an error using Flask when following a tutorial on using Flash. Since I am a basic Python programmer I don't understand why or what is wrong with it. So if you don't mind explaining it or adding a explaining link. Error printed on…
wondercoll
  • 339
  • 1
  • 4
  • 15
9
votes
4 answers

how to connect to sqlite from sqlalchemy

I have a sqlite db in my home dir. stephen@stephen-AO725:~$ pwd /home/stephen stephen@stephen-AO725:~$ sqlite db1 SQLite version 2.8.17 Enter ".help" for instructions sqlite> select * from test ...> ; 3|4 5|6 sqlite> .quit when I try to connect…
Stephen
  • 550
  • 1
  • 4
  • 15
9
votes
1 answer

SQLAlchemy warning: column won't be part of the declarative mapping

trying to build to-do list app with python , flask and sqlalchemy; when creating a table to database i got this error i run this command 'from app import db' I receive this warning…