Matthew
It needs to lookup with two attributes
stsouko
Del
Alexander
So you have a list of pairs with attribute values?
Matthew
Yes
Matthew
Ideally I'd be able to zip up the list of pairs, and the select results, so it's easy to see which pairs have a result
Alexander
Ideally, it should be expressed as A.select(lambda a: (a.z, a.b) in pairs_list) but I checked, and right now it is not working. I think we need to fix it. #todo In the mean time, you can use a query with a raw sql fragment of the following structure: conditions = '(%s)' % ' or '.join( 'a.z = %d and a.b = %d' % (val1, val2) for val1, val2 in pairs_list) A.select(lambda a: raw_sql(conditions)) If values are not numeric, it may be better to use $parameters to avoid SQL injection: conditions = '(%s)' % ' or '.join( 'a.z = $(pairs_list[%d][0]) and a.b = $(pairs_list[%d][1])' % (i, i) for i, (val1, val2) in enumerate(pairs_list)) A.select(lambda a: raw_sql(conditions))
Anonymous
Is it possible to bind to a sqlite database from a stringio buffer?
Matthew
Thanks Alexander, I look forward to the fix :)
Matthew
You can do in-memory SQLite and probably put it into a StringIO object, what is your use case?
Anonymous
I want to do it the other way around.
Anonymous
I have an encrypted db file which I decrypt and I don't want to store the decrypted data on disk. I need a way to interface with that data.
Matthew
https://stackoverflow.com/a/3850259/964375
Matthew
encrypt the result of iterdump, load the decrypted data into an in-memory sqlite file
Anonymous
How do i bind to an in memory database with pony? : memory: creates a new one from what I've seen
Anonymous
Which is why I wanted to use a string io file. The problem being pony uses a filename, not a file object.
Alexander
As far as I know, sqlite does not allow to bind to existing in-memory database. You can bind pony to a new in-memory database and then load data into it. You can obtain native sqlite connection from the database object using db.get_connection()
Anonymous
I have no idea
Anonymous
The problem with loading to the new memory database is that it just shifts the problem upstream
Alexander
db = Database('sqlite', ':memory:') define_entities(db) db.generate_mapping(create_tables=False) connection = db.get_connection() load_data(connection, backup_data)
Anonymous
Sorry I can't find load_data in the docs, what kind of format does backup_data have to be in?
Alexander
It just some pseudocode. I mean, Pony allows you to obtain native sqlite3 connection to empty in-memory database, and then you can write a function which load decripted data into it. I don't think that python sqlite3 module accepts file descriptor instead of file name, so probably you need to load decrypted data into empty in-memory database, not the other way around
Anonymous
Yeah seems sqlite doesn't allow it. But yeah it just shifts the problem upstream since I have to deal with the data myself it defeats the purpose of using pony for my use case :(.
Anonymous
Either way, thanks for the insights, maybe I need to find a simpler way to deal with encryption
Alexander
load_data may be some generic function, like in that stackoverflow example that Matthew posted. It is not necessary for load_data to know definition of entities. But yeah, may be there is some easier approach to encryption
Matthew
SQLite likely doesn't allow Python file handles because it is a C library
Anonymous
Ooh yeah I misunderstood your first reply, I thought you meant load the dump into an in memory file and then connect to it using pony.
Anonymous
The dump solution is great actually
Anonymous
Thanks for the help =)
Matthew
No problem :)
J
Do you know of an SQLite group?
Lucky
Which is why I wanted to use a string io file. The problem being pony uses a filename, not a file object.
Yeah had a similar issue too, and tried to use the filename operator: See https://github.com/ponyorm/pony/issues/260
Lucky
TIL There even is a PR for that
Alexander
@luckydonald nice doggo btw
Lucky
Is from @get_happiness Edit This one: https://t.me/get_happiness/586
Jim
Hello. I have a one to one relationship. Following the doc example : How do I create a default "Passeport" automaticaly each time I create a new "Person" ? Maybe in the init or Person or anything else ?
Matthew
def before_insert(self): # make Passport object here
Matthew
that method is for Person
Jim
damn !! that one should not have been asked. sorry and thanks
Matthew
No problem :)
Matthew
I have found some weird behaviour in Pony I think
Matthew
If I have a list of objects:
Matthew
mappings = [X[1], X[2], X[3]]
Matthew
if I then do:
Matthew
for x in mappings: print len(x.y)
Matthew
(where y is a Set)
Matthew
Pony will issue one big query at the start of the loop, with an in statement, for all mappings objects
Matthew
so the first query is slow, then the others are very fast
Matthew
is this intentional?
Matthew
Nevermind, was working around a problem which turned out to be a missing index on a foreign key :)
Henri
@akozlovsky Is it possible to use GraphQL together with PonyORM or are their plans to support it?
Alexander
Vitalii Abetkin wrote the initial version of Pony GraphQL API some time ago, you can find it here: https://github.com/abetkin/pony_graphql If I remember correctly, it does not fully provide GraphQL cursor API Currently there are no plans to officially support GraphQL with PonyORM
Henri
Thanks!
Oleg
Hi there! 🙂
Alexander
You are welcome!
Oleg
Does pony support asyncio?
Alexander
Not yet. Maybe later we will add support of it. But it is possible to work with database synchronously even when using asyncio. For simple queries, like, retrieve object by primary key, it will be even faster
Lucky
Does pony support asyncio?
Can't you call normal functions with `await` from an async function?
Lucky
I'm new to async, so I really don't know. Tried async yesterday for the first time
Oleg
Can't you call normal functions with `await` from an async function?
Nope, you can’t await func 🙂 rtm If you need to call non-aio func - just call it 🙂
Adam
i use pony with asyncio
Oleg
i use pony with asyncio
I can use sync usage with async too 🙂
Jim
Hello, what's the usual way to deal with pictures, vidéo, pdf with pony ? Just a string with path to file ?
Alexander
Yes, it is typically better to store them outside of database. Pony does not have specific "filename" datatype, so you can use str datatype instead
Jim
ok thanks
Matthew
I upgraded to postgres 10, and no problems with pony
Matthew
FYI
Alexander
Thanks, cool
Henri
If I have a query like result = select(article for article in Article  for author in article.author  if self.user in author.followers) Makes it sense to simplify it to result = select(article for article in Article  if self.user in article.author.followers) Or is this not an optimization?
Matthew
I think the latter is fine
Matthew
use sql_debug and see what sql is generated
Alexander
I think it should be about equivalent. Currently Pony translates in to SQL subquery with in. For MySQL it may be more efficient to use join instead of subquery with in. You can suggest Pony to use join by using the following syntax: result = select(article for article in Article for author in article.author if JOIN(self.user in author.followers)) or result = select(article for article in Article if JOIN(self.user in article.author.followers))
Alexander
JOIN is just a hint which may be ignored by Pony for some complex queries
Henri
I use PostgresQL.
Alexander
Then it is not necessary
Henri
And SQLite for testing.
Henri
Thanks. So I will use the simplified form.
Alexander
I think the simplest form is result = Article.select(lambda a: self.user in a.author.followers)