I'd like to print the code a particular session generates and sends to the DB. I've seen this question but it echoes all the SQL that SQLAlchemy generates. I want just the SQL generated on behalf of a particular session. Bonus points if it can be printed before it is actually send to the DB (like we can with the Query object)
I know I can print the SQL of a query, but in this situation
a = Table(id=1)
session.add(a)
b = Table(id=4)
session.add(a)
b.column = 5
session.commit()
there is no query to print, but the session is beginning a transaction, inserting rows and committing the transaction.
I'd like the session to print to stdout (or a file, if that's not possible) the SQL commands it's sending to the DB.
The (immaginary) SQL I expect to find in the console is
BEGIN TRANSACTION
INSERT INTO Table (id) VALUES (1);
INSERT INTO Table (id, column) VALUES (4,5);
COMMIT TRANSACTION
I fully understand that the lines might not all come together and might be interleaved by other code prints (the session is beginning the transaction first, then later the code trigger the flush of the insert, and as last thing the session issues the commit)
Is that possible?
Related
I am using SQLAlchemy and pg8000 to connect to a Postgres database.
I have checked table pg_stat_activity, which shows me a few select queries in 'idle in transaction' state, many of those. But the application much more reads than writes, that is, inserts are few and far between.
I suspect that a new transaction is created for each query, even for simple select statements.
Is it possible to run a read-only query without the need for a transaction? So that it does not need to be committed/rolled back?
Currently, the app runs its queries with method sqlalchemy.engine.Engine.execute for CRUD operations and cursors for calling stored procedures. How should I update these method calls to indicate I want some of them not to start transactions?
Ours is flask app, sql alchemy, sql server based stack. We are seeing a weird issue issue in our production.
While doing an insert operation, we are getting
sqlalchemy.orm.exc:ObjectDeletedError
In database we see that the entity did not got created. We have a trigger function on our insert which has lot of PRINT statements in it.
Sequence of events -
obj = Object()
session.add(obj)
session.commit()
obj.id # sqlalchemy.orm.exc:ObjectDeletedError
Issue is not seen if we remove the PRINT statements.
Whats the co relation of print statements in triggers and sql alchemy?
Also the issue is not seen in our test envs but only on production.
What is the difference between executing raw SQL on the SQLAlchemy engine and the session? Specifically against a MSSQL database.
engine.execute('DELETE FROM MyTable WHERE MyId IN(1, 2, 3)')
versus
session.execute('DELETE FROM MyTable WHERE MyId IN(1, 2, 3)')
I've noticed that executing the SQL on the session, cause MSSQL to 'hang'.
Perhaps someone has an idea on how these two executions are different, or perhaps someone can point me where to further investigate.
The reason why MSSQL Server was hanging, was not because of the difference between calling execute on the engine or the session, but because a delete was being called on the table, without a commit, and then a subsequent read.
I'm currently using SQLAlchemy with two distinct session objects. In one object, I am inserting rows into a mysql database. In the other session I am querying that database for the max row id. However, the second session is not querying the latest from the database. If I query the database manually, I see the correct, higher max row id.
How can I force the second session to query the live database?
The first session needs to commit to flush changes to the database.
first_session.commit()
Session holds all the objects in memory and flushes them together to the database (lazy loading, for efficiency). Thus the changes made by first_session are not visible to the second_session which is reading data from the database.
Had a similar problem, for some reason i had to commit both sessions. Even the one that is only reading.
This might be a problem with my code though, cannot use same session as it the code will run on different machines. Also documentation of SQLalchemy says that each session should be used by one thread only, although 1 reading and 1 writing should not be a problem.
I'm trying to execute TSQL queries in a remote MSSQL database by using SQLAlchemy and pymssql. I've tested my procedural query directly in the database and it works as intended, it also works if I run it directly through pymssql. If I run a regular one liner queries such as:
select table_name from INFORMATION_SCHEMA.tables
Through SQLAlchemy this also works as it should. But when I try to execute the following TSQL query it does not actually create the table:
IF NOT EXISTS (SELECT *
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME = 'SOME_TABLE')
BEGIN
CREATE TABLE SOME_TABLE (SOME_TEXT VARCHAR(255), SOME_TIME DATETIME)
END
it runs it as it was successful and if I try to read the result it from the execution it gives me "Resource already closed error" as expected since it is a CREATE query. However if I try to add data to table 'SOME_TABLE' it pukes at me and says that the table does not exist. Feels like it is only uploading the query as a function but never executes it. Any ideas? Or even better; TSQL queries that actually works when executing with SQLAlchemy and pymssql.
Thanks,
You need to commit your pending changes in the Session. For basic understanding of the process read Using the Session. Quick solution:
session.commit()
TIME and TEXT are reserve words.
I do not know how SQL Alchemy or pymmsql talks to SQL Server. Either native client or ODBC. It eventually all boils down to a tabular data stream (TDS) over a network protocol like TCP/IP.
Check out the reserve word list on TECHNET.
-- Create table ?
IF OBJECT_ID('DBO.SOMETABLE') IS NULL
CREATE TABLE DBO.SOMETABLE (MY_TEXT VARCHAR(255), MY_TIME DATETIME);
I use the OBJECT_ID function since it is less typing.
But NOT EXITS works with both SELECT FROM the sys.objects or information_schema.tables with correct WHERE clauses.