1

I've found a question on how to insert values from a table into another table:

SQLAlchemy: Inserting the results of a query into another table

My query is is slightly different.

When running a SQL you can do something like this:

insert into table1(col1, col2)
   select some_constant_value_or_variable, col2 from table2;

Is there a way to do this in SQLAlchemy

Karlson
  • 2,958
  • 1
  • 21
  • 48
  • Is your question regarding using the SQLAlchemy ORM? If not, you could just execute the the SQL query to construct the new table using `cursor.execute(...)` – R. Arctor Oct 13 '20 at 21:57
  • @R.Arctor It is. If I wanted to use straight connection I could have run everything without it. – Karlson Oct 13 '20 at 22:03
  • @Karlson - How is your desired SQL statement different from the one in [this answer](https://stackoverflow.com/a/22290468/2144390)? Is it the `some_constant_value_or_variable` bit? – Gord Thompson Oct 13 '20 at 22:55
  • @GordThompson Because `some_constant_value_or_variable` is a constant that may but more then likely will not exist inside a database. More then likely this will be a python variable. – Karlson Oct 13 '20 at 23:02
  • Does this help? https://docs.sqlalchemy.org/en/13/core/sqlelement.html#sqlalchemy.sql.expression.literal_column – Gord Thompson Oct 13 '20 at 23:09
  • @GordThompson Not by itself. I can't seem to have a `literal_column` to return value under a particular label. – Karlson Oct 13 '20 at 23:15
  • @Karlson - [This](https://pastebin.com/k3Jg9vMc) works for me. – Gord Thompson Oct 14 '20 at 01:19
  • Can you post the answer or the call? – Karlson Oct 14 '20 at 01:20

0 Answers0