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

Data migration from MS SQL to PostgreSQL using SQLAlchemy

TL;DR I want to migrate data from a MS SQL Server + ArcSDE to a PostgreSQL + PostGIS, ideally using SQLAlchemy. I am using SQLAlchemy 1.0.11 to migrate an existing database from MS SQL 2012 to PostgreSQL 9.2 (upgrade to 9.5 planned). I've been…
iled
  • 2,142
  • 3
  • 31
  • 43
9
votes
1 answer

db.create_all() doesn't create tables defined in separate file

I am trying to create the tables for my models, which are defined in a separate module from my app. I call db.create_all(), but no tables are created and there are no errors. I've defined the models and imported them before calling create_all. …
user1592380
  • 34,265
  • 92
  • 284
  • 515
9
votes
1 answer

SQLAlchemy func.count with filter

I'm using a framework that does pagination like this: def get_count_query(self): return self.session.query(func.count('*')).select_from(self.model) def paginate(self): ... ... count = self.get_count_query.scalar() ... I…
fasouto
  • 4,386
  • 3
  • 30
  • 66
9
votes
1 answer

Using Flask-SQLAlchemy in Multiple uWSGI Processes

I've been fighting a persistent error in my Flask application: OperationalError: (_mysql_exceptions.OperationalError) (2006, 'MySQL server has gone away') I'm using a mySQL server instance and the Flask-SQLAlchemy module. I double checked the…
Vytas Bradunas
  • 626
  • 1
  • 7
  • 15
9
votes
1 answer

Flask API not receiving requests all of a sudden

I am trying to create a REST API in Flask. The thing is it runs perfectly for a few days and then all of a sudden it STOPS receiving requests altogether. Forget about not responding to requests; it just doesn't receive any requests at the first…
90abyss
  • 7,037
  • 19
  • 63
  • 94
9
votes
1 answer

unittesting sqlalchemy BinaryExpressions

I'm writing some unittests for some code that uses SQLAlchemy. I want to test filter calls, but it seems that SQLAlchemy BinaryExpression objects created with the same arguments don't compare equal: AssertionError: Expected call:…
swizzard
  • 1,037
  • 1
  • 12
  • 28
9
votes
1 answer

How to set default value using SqlAlchemy_Utils ChoiceType

I just migrated from NoSQL to SQL, and I'm rather new to the SqlAlchemy ORM. In my use case, I need a field in models to be able to store a given choice set: # models.py from sqlalchemy.ext.declarative import declarative_base from…
benjaminz
  • 3,118
  • 3
  • 35
  • 47
9
votes
1 answer

How to `SET CONSTRAINTS ... DEFERRED` in SQLALchemy Core

I am using SQLAlchemy and PostgreSQL. Postgres supports doing deferred constraints, which allows us to postpone checking the constraints on a table until the end of the transaction. For example, in SQLAlchemy, I might define a table like…
therealrootuser
  • 10,215
  • 7
  • 31
  • 46
9
votes
1 answer

Select batch of rows sqlalchemy mysql

I have a MySQL database with a few thousand forum posts + text. I would like to grab them in batches, say 1000 at a time, and do stuff to them in python3. My single post query looks like: pquery = session.query(Post).\ …
nicolashahn
  • 539
  • 7
  • 19
9
votes
2 answers

sqlalchemy date error. Argument 'arg' is expected STR but got INT

I have a database schema (which i cannot change) with dates. they are defined as: +---------------------+------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra …
Pat R Ellery
  • 1,696
  • 3
  • 22
  • 40
9
votes
1 answer

How to find all of an SQLAlchemy model's hybrid attributes?

I need to find a way to get a list of all of an SQLAlchemy models hybrid properties. For relationships on a Person instance I can do something like: from sqlalchemy.inspection import inspect inspect(person.__class__).relationships Is there…
Jakobovski
  • 3,203
  • 1
  • 31
  • 38
9
votes
2 answers

SQLAlchemy query shows error "Can't join table/selectable 'workflows' to itself"

I wrote a SQL query that I'm trying to port to SQLAlchemy, but receive the following error: sqlalchemy.exc.InvalidRequestError: Can't join table/selectable 'workflows' to itself SQL (working): SELECT w.user_id, COUNT(l.id) FROM logs as…
okoboko
  • 4,332
  • 8
  • 40
  • 67
9
votes
2 answers

python 2.7: no module named configparser

I'm trying to run db_create.py in Flask, and am getting the following error: from six.moves.configparser import ConfigParser ImportError: No module named configparser Even after a successfull pip install, the same error code comes up. I'm seeing…
fstopzero
  • 1,073
  • 2
  • 10
  • 24
9
votes
1 answer

setting server_default in sqlalchemy fails

this is the stuff that I want SQLAlchemy to do: blesk_dev=# alter table address add column reg_at timestamp without time zone default (now() at time zone 'utc'); ALTER TABLE that is, I want to set a default UTC time for a column. In pure psql, as…
kurtgn
  • 8,140
  • 13
  • 55
  • 91
9
votes
3 answers

Clear postgresql and alembic and start over from scratch

Everything I found about this via searching was either wrong or incomplete in some way. So, how do I: delete everything in my postgresql database delete all my alembic revisions make it so that my database is 100% like new
kai
  • 1,288
  • 3
  • 12
  • 24