Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insert data into specific table (sqlalchemy)

It's pretty easy to insert data into a database by using sqlalchemy.

address.name = 'Joe'
address.age = 26
session.add(address)

But actually I have three tables - how can I specify the table I want to insert my data in?

like image 403
jokober Avatar asked Nov 02 '12 23:11

jokober


People also ask

How to infer the value of a table in SQLAlchemy?

For example, assuming your foo table contains an id column and is mapped to a Foo class: Since SQLAlchemy picked up the value for x.id without issuing another query, we can infer that it got the value directly from the INSERT statement.

Why does SQLAlchemy not support bulk inserts?

I believe the underlying reason is that SQLAlchemy needs to keep track of each object's identity (i.e., new primary keys), and bulk inserts interfere with that. For example, assuming your foo table contains an id column and is mapped to a Foo class:

Where can I find more information about the SQLAlchemy API?

You may check out the related API usage on the sidebar. You may also want to check out all available functions/classes of the module sqlalchemy , or try the search function .

What happened to the UPDATE statement in SQLAlchemy?

The rendered UPDATE statement will emit the SET clause for each referenced column maintaining this order. Deprecated since version 1.4: The update.preserve_parameter_orderparameter will be removed in SQLAlchemy 2.0. Use the Update.ordered_values()method with a list of tuples. New in version 1.0.10. See also


1 Answers

I solved the problem by using the sql expression language to add the new row:

engine.execute(table_addresses.insert(), name='Joe', age=20)
engine.execute(table_contacts.insert(), email='[email protected]', cellnumber='267534320')

Normaly I use the ORM but in my case it is more convinient.

like image 159
jokober Avatar answered Sep 29 '22 02:09

jokober