Alexander
It is the same query
Henri
I will try.
Henri
@akozlovsky The whole query looks like def query(self): result = select(article for article in Article if self.user in article.author.followers) result = result.sort_by(desc(Article.created_at)) if self.limit: result = result.limit(self.limit, offset=self.offset) return result[:]Is your proposal here possible?
Alexander
absolutely def query(self): result = Article.select(lambda a: self.user in a.author.followers) result = result.sort_by(desc(Article.created_at)) if self.limit: result = result.limit(self.limit, offset=self.offset) return result[:]
Henri
Great! Tests are passing...
Alexander
👍
Matthew
Hi, I have a TransactionIntegrityError on a flask view with Pony, when a record is created with a unique constraint, and the user submits two requests in parallel
Matthew
Should I be setting SERIALIZABLE?
Matthew
If there's a conflict, the record exists, so I want to return my success message
Matthew
Could I catch an explicity commit() in a try / except block?
Matthew
try: commit() except: pass
Alexander
I'm not sure SERIALIZABLE can solve this
Alexander
I think you can solve this by using PostgreSQL INSERT ... ON CONFLICT (...) DO UPDATE ... statement https://www.postgresql.org/docs/current/static/sql-insert.html#SQL-ON-CONFLICT Pony does not support it yet, but you can write raw sql query db.execute("insert ...")
Anonymous
Hey guys, I'm wondering what's the best practice for password management when connecting to a database with pony? Leaving it in clear in the code seems dangerous, how do you guys deal with this?
stsouko
Use config files
Anonymous
Is that the standard way? It doesn't feel great security wise, I feel like one of my users could accidentally share theirs.
Jim
use python-dotenv
stsouko
U can set file attr 640 in unix
Anonymous
Oh that's great, didn't think to store them as environment variables, I'm gonna do that thank you both =)!
Roman
Hi guys. Can you recommend good swagger library for flask? Most of them seems abandoned.
stsouko
Flask swagger 2
Alexey
do you mean this one? https://github.com/soerface/flask-restful-swagger-2.0
Permalink Bot
do you mean this one? https://github.com/soerface/flask-restful-swagger-2.0
Permanent link to the soerface/flask-restful-swagger-2.0 project you mentioned. (?)
stsouko
Yes
Roman
this project is not maintained
Terrence
Hello, I typed up my question about PonyORM here - https://stackoverflow.com/questions/50875270/schema-migrations-in-ponyorm And there is an old, unanswered question here: https://stackoverflow.com/questions/50016739/mock-pony-orm-entity
Alexander
Hi Terrence! Thanks for poining out, I'll answer the questions
Alexander
Done
Anonymous
Как дел?)
Alexander
Good )
Jim
Hi. I want to save 3 files en disk, they correspond to a Document entity. The thing is for each file I wanna be sure that writting to disk ANd to database succeeds. before_insert or after_insert can't do that. I was thinking a bout a context manager. Do you see another way to do it ?
Jim
this what I want to accomplish : python @db_session def save_document(path): some_files = [...] new_entity =[] for file in some_files: try: d = Document(*args) d.flush() savefile except orm.OrmError: raise someother error except IOError: rollback raise someerror else: new_entity .append(d) return new_entity
Matthew
So you want it to be one atomic transaction?
Jim
Not necessarily. I do not want save file if database fails and i don't want save to database if file save fails. I don't want every thing fail, if only one file fails. the thing is that the underlying function is already inside a db_session
Jim
if flush() succeeds, does it mean that commit() will ?
Matthew
I think the problem fundamentally is that either system (filesystem / database) can fail independently
Matthew
You could write to database, then write to file, and then if writing to file fails, remove your database record?
Matthew
or have a flag for file written in the database table
stsouko
Flush can be roll backed
stsouko
If exception raised
Jim
this is why I propose flush better than commit
Alexander
I think it may be something like that: def save_file(filename, saved_files, ...): <do save> saved_files.append(filename) def save_document(path): files_to_save = [...] saved_files = [] new_entities = [] try: with db_session: for file in files_to_save: d = Document(*args) new_entities.append(d) d.flush() save_file(file, saved_files, ...) except: for filename in saved_files: remove_file(filename) raise return new_entities
Jim
is it a problem if save_document is already decorated by @db_session ?
Alexander
In that case inner db_session will be ignored, so commit to the database happens too late. You can do manual commit: def save_file(filename, saved_files, ...): <do save> saved_files.append(filename) @db_session def save_document(path): files_to_save = [...] saved_files = [] new_entities = [] try: for file in files_to_save: d = Document(*args) new_entities.append(d) d.flush() save_file(file, saved_files, ...) commit() except: for filename in saved_files: remove_file(filename) raise return new_entities
Jim
thank you guys
Alexander
Nice. "Hot chicks" join our comminity, not a spam for sure.
Alexander
Alexander, It's a sexism. It may be a real developer, why do you now. Btw, some time ago your avatar was an anime girl with panties on her head. So, not be so fast with conclusions )
Henri
Hmm one after the other and all written in upper case. For sure spam.
Alexander
Shame on me
Grigory
Hi, everyone!
Grigory
I'm trying to use buffer as PrimaryKey. PonyORM allows this, but select always fails afterwards.
Grigory
from pony import orm db = orm.Database() class Bla(db.Entity): smt = orm.PrimaryKey(buffer, auto=False) db.bind(provider='sqlite', filename=':memory:') db.generate_mapping(create_tables=True) a = Bla(smt=str(0x01)*20) q = orm.select(p for p in Bla)[:]
Grigory
Python 2.7 mumbles something about TypeError: writable buffers are not hashable
Grigory
Does anyone have some thoughts about this problem?
stsouko
Don't use mutable types as pk
Grigory
but how I can use binary type as pk then?
stsouko
Str in 2.7 is bytes string
stsouko
In python 3 u can use bytes
Grigory
I tried string, but I get 'can't convert to unicode' error
Grigory
ValueError: Value for attribute MetadataGossip.sig cannot be converted to unicode: '8\x13D\xb2\xcd3\xf62\xa2\xbe\x83\xa3ca<3\xb3u\xe5M\xb9n\td\x9d}\xf8K\x8do\xad\xea+\x94\xfc\x90Tl...
Grigory
i.e. I can't create the entry
Grigory
but that's another problem
stsouko
Are you using python 2.7
Grigory
Python 2.7.14 (default, Jan 17 2018, 14:37:42) [GCC 6.3.0 20170406] on linux2
stsouko
Try to set attr as bytes
stsouko
I'm using bytes type for binary data on python 3
Grigory
still cannot be converted to unicode
Grigory
hm
stsouko
Error looks like python try to convert bytes to Unicode with invalid encodings
Grigory
funny thing, PonyORM seems to expect the value to be unicode. Therefore, It is impossible to store true binary data in str with Pony.
Grigory
```
Grigory
``` from pony import orm db = orm.Database() class Bla(db.Entity): smt = orm.PrimaryKey(str, auto=False) db.bind(provider='sqlite', filename=':memory:') db.generate_mapping(create_tables=True) a = Bla(smt=str('\x13D\xb2\xcd3\xf62\xa2\xbe')) q = orm.select(p for p in Bla)[:] ```
Grigory
triggers it
stsouko
PrimaryKey(bytes, auto=False)
stsouko
Try this
Grigory
same result
Grigory
'cant convert'
Henri
From the docs: > Starting with the Pony Release 0.6 the attributes of str type in Python 2 behave as if they were declared as unicode attributes. There is no difference now if you specify str or unicode as the attribute type – you will have unicode string in Python and in the database.
Grigory
yep. Unfortunately, the docs don't tell that its impossible to use buffer as PrimaryKey