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
10
votes
1 answer

SQLAlchemy raw sql vs expression language statements

When inserting multiple rows in a MySQL-DB via a SQLA-Expression-Language statement, f.e. Foo.__table__.insert().execute([{'bar': 1}, {'bar': 2}, {'bar': 3}]) it´s extremly slow, when compared to the execution of a "raw" sql statement for the same…
dorvak
  • 9,219
  • 4
  • 34
  • 43
10
votes
1 answer

DELIMITER / Creating a trigger in SQLAlchemy

I need to create a BEFORE INSERT trigger in SQLAlchemy: DELIMITER | CREATE TRIGGER set_rank BEFORE INSERT ON authors FOR EACH ROW BEGIN IF NEW.rank = 0 THEN SET NEW.rank = (SELECT IFNULL(MAX(a.rank),0) + 1 FROM…
Kijewski
  • 25,517
  • 12
  • 101
  • 143
9
votes
2 answers

How I do to update data on many-to-many with WTForms and SQLAlchemy?

I've a small problem on my App build with Flask Framework. I'm trying to create a simple User + Permissions module. To archive it, I've a many-to-many relation between Users and Permissions table. Here is my model, form and route Model user_perm =…
yvan
  • 938
  • 9
  • 18
9
votes
1 answer

Filtering with joined tables

I'm trying to get some query performance improved, but the generated query does not look the way I expect it to. The results are retrieved using: query = session.query(SomeModel). options(joinedload_all('foo.bar')). …
viraptor
  • 33,322
  • 10
  • 107
  • 191
9
votes
3 answers

Unicode Problem with SQLAlchemy

I know I'm having a problem with a conversion from Unicode but I'm not sure where it's happening. I'm extracting data about a recent Eruopean trip from a directory of HTML files. Some of the location names have non-ASCII characters (such as é, ô,…
Dave Forgac
  • 3,146
  • 7
  • 39
  • 54
9
votes
1 answer

creating a temporary table from a query using sqlalchemy orm

I can create a temporary table this way: session.execute("CREATE TABLE temptable SELECT existingtable.id, " "existingtable.column2 FROM existingtable WHERE existingtable.id<100000") but the new table is unreadable because it says it has no…
Paul
  • 2,973
  • 6
  • 31
  • 40
9
votes
2 answers

connecting sqlalchemy to MSAccess

How can I connect to MS Access with SQLAlchemy? In their website, it says connection string is access+pyodbc. Does that mean that I need to have pyodbc for the connection? Since I am a newbie, please be gentle.
Jack_of_All_Trades
  • 10,942
  • 18
  • 58
  • 88
9
votes
1 answer

Executing an Oracle stored procedure returning a REF CURSOR using SqlAlchemy

I have a stored procedure that I have defined in Oracle. In that procedure I need to return a recordset. To do this, I am using the SYS_REFCURSOR, which works great inside of Oracle (and with cx_Oracle, for that matter). In my application I am using…
zmouser
  • 183
  • 1
  • 2
  • 8
9
votes
2 answers

AttributeError: 'Connection' object has no attribute 'connect' when use df.to_sql()

I am trying to store data retrieved from a website into MySQL database via a pandas data frame. However, when I make the function call df.to_sql(), the compiler give me an error message saying: AttributeError: 'Connection' object has no attribute…
FxxkDogeCoins
  • 115
  • 1
  • 5
9
votes
2 answers

SQLAlchemy ORDER BY FIELD()

I am trying to sort an SQLAlchemy ORM object by a field, but with a specific order of the values (which is neither ascending or descending). If I was doing this query on MySQL, it would look like; SELECT letter FROM alphabet_table WHERE letter in…
Adam Morris
  • 8,265
  • 12
  • 45
  • 68
9
votes
4 answers

SQLAlchemy many-to-many orphan deletion

I'm trying to use SQLAlchemy to implement a basic users-groups model where users can have multiple groups and groups can have multiple users. When a group becomes empty, I want the group to be deleted, (along with other things associated with the…
Jack Edmonds
  • 31,931
  • 18
  • 65
  • 77
9
votes
2 answers

AttributeError: 'unicode' object has no attribute '_sa_instance_state'

I'm just learning how to use SQLAlchemy. I'm trying to do the following, but storing title and link in two separate tables: temp = Submissions(title=u'Facebook Homepage',…
Jonathan Ong
  • 19,927
  • 17
  • 79
  • 118
9
votes
3 answers

SQLAlchemy - limit the joinedloaded results

Models: class Team(Base): id = Column(Integer, primary_key=True) name = Column(String, nullable=False) players = relationship("Player", backref="team") class Player(Base): id = Column(Integer, primary_key=True) name =…
Jashwant
  • 28,410
  • 16
  • 70
  • 105
9
votes
3 answers

SQLModel: sqlalchemy.exc.ArgumentError: Column expression or FROM clause expected,

I am using the SQLModel library to do a simple select() like described on their official website. However I am getting Column expression or FROM clause expected error message from typing import Optional from sqlmodel import Field, Session,…
joooet
  • 105
  • 1
  • 1
  • 5
9
votes
2 answers

How to properly handle join results from SQLAlchemy with Pydantic/FastAPI

I would like a piece of advice on handling the result of a join operation performed in SQLAlchemy and do the serialization with Pydantic (in FastAPI). If I am not mistaken, the result of the join on two table leads to a list of tuples of SQLAlchemy…
Flavien Lambert
  • 690
  • 1
  • 9
  • 22