@ponyorm

Страница 64 из 75
Jim
24.06.2018
21:20:20
following my question about writing file to disk I finally did It with a classmethod of my entity Document. @classmethod def new(cls, old_filename, content_type, stream, acte): d = Document( filename=cls.get_new_filename(content_type), content_type=content_type, acte=acte, ) try: d.write(stream) except Exception as exc: d.delete() return Exception(str(exc)) return d # and in postdocument() for f in vf: d = Document.new( old_filename=f["filename"], content_type=f["content_type"], stream=f["content"], acte=acte, ) if isinstance(d, Document): entities.append(d) else: errors.append(f"Une erreur s'est produite avec {f['filename']}")

Alexander
24.06.2018
21:23:35
return Exception part looks a bit strange

Jim
24.06.2018
21:25:54
indeed ? I initially checked isinstance(d, Exception) but I removed it. Il will change it

Alexander
24.06.2018
21:26:21
Also, in case when one of several documents raises an error, other documents will be committed anyway. It may be desired behavior or not, depending on application

Google
Jim
24.06.2018
21:27:29
yes it's desired behaviour, I return a "mixed" response if some fail.

Anatolii
26.06.2018
12:04:00
Hi, I have a question regarding sequence_name attribute for PrimaryKey. I'm defining it while declaring entity: class REGION(db.Entity): REG_RECID = pny.PrimaryKey( int, sequence_name='SEQ_REGION_RECID', auto=True) REG_CODE = pny.Required(str, unique=True) But it is omitted on show(): class REGION(Entity): REG_RECID = PrimaryKey(int, auto=True) REG_CODE = Required(str, unique=True) and sequence is not being called while instance creation/commit: with pny.db_session: r2 = GC_REGION(GC_REG_CODE='5') pny.commit() pony.orm.core.TransactionIntegrityError: Object REGION[new:1] cannot be stored in the database. IntegrityError: ORA-01400: cannot insert NULL into ("TEST"."REGION"."REG_RECID") What I have missed?

with pny.db_session: r2 = REGION(REG_CODE='5') pny.commit() Sorry wrong table above )

Alexander
26.06.2018
12:06:11
Hi Anatolii! What database do you use?

Anatolii
26.06.2018
12:06:32
Oracle 12

Alexander
26.06.2018
12:20:01
The show may omiss some elements from the original description. But I think we need to add sequence_name value for it. Thanks for poining this out! #todo In order to use the sequence the should be a corresponding trigger. Pony should create it when generate_mapping is called with create_tables=True option. It seems that for some reason the trigger was not created in your case.

Anatolii
26.06.2018
12:21:31
Hmm. I’ll recheck trigger in database

It seems that i have set create tables to False, now all i ok. Sorry )

Alexander
26.06.2018
13:55:33
No problem )

Alexander
27.06.2018
13:13:10
Guys, this is my new telegram account. I need to create it, because I forgot password from my previous account, and cannot recover it

Alexander
27.06.2018
13:13:41
I confirm this from my previous account :)

stsouko
27.06.2018
14:28:35
Second time?

Save it to paper

Google
Alexander
27.06.2018
14:29:28
No, it is the first time, I just now switched to the new account

Grigori
03.07.2018
14:50:38
Hi guys! I really want to use SQLite FTS3/4 with Pony. What options do I have for this? Is it possible at all? I want FTS table to be separate from my primary table, and I want to do JOINs on them.

Alexander
03.07.2018
14:53:51
If I understand correctly. it should be possible right now

You probably need SQLite binaries with FTS support enabled

Grigori
03.07.2018
14:54:37
I already got them ))

the question is: how to formulate the query?

Matthew
03.07.2018
14:54:57
Does your version of python have it enabled? I think that's the SQLite that pony will use

Grigori
03.07.2018
14:55:09
yeah I checked it

Alexander
03.07.2018
14:55:15
You can define "virtual" entity for FTS table just as a usual entity

For MATCH operation you can use raw SQL fragments

like:

query = 'some search string' select(obj for obj in MyFtsEntity if raw_sql('obj.somecolumn MATCH $query'))

Grigori
03.07.2018
14:57:46
wow! That's cool!

Thank you sir!

Alexander
03.07.2018
14:57:57
Sure

Grigori
04.07.2018
11:07:50
Another question: I created a trigger in SQLite on row delete from my main table to delete the corresponding row from FTS table. The trigger works OK if I use direct SQL statement to delete the original row, but Pony fails to .delete() the original object mumbling something about: OperationalError: SQL logic error Am I doing something wrong, or Pony does not play well with triggers on delete?

of course, there is always an option to use Pony's internal after_delete() hook, but I'd rather do it in SQLite itself.

Alexander
04.07.2018
11:11:19
OperationalError: SQL logic error is some SQLite error. You need to look at previous SQL command to understand the reason of the error. You can set_sql_debug(True) or use with sql_debugging: decorator

Grigori
04.07.2018
11:13:15
I'll try it out in a minute. But what *really* happens on calling delete() in Pony? At what moment does the actual `DELETE`is run by the database?

pony.orm.sql: INFO: SELECT "g"."rowid", "g"."type", "g"."signature", "g"."infohash", "g"."title", "g"."size", "g"."timestamp", "g"."torrent_date", "g"."tc_pointer", "g"."public_key", "g"."tags", "g"."addition_timestamp", "g"."version" FROM "MetadataGossip" "g" WHERE "g"."public_key" = ? AND "g"."type" = ? pony.orm.sql: INFO: DELETE FROM "MetadataGossip" WHERE "rowid" = ?

Google
Grigori
04.07.2018
11:19:24
that was the last two entries in the log

the main table is MetadataGossip

Alexander
04.07.2018
11:20:27
When you call obj.delete() the object is marked for deletion. At this moment pony traverse dependent objects and call delete on them recursively. When next flush command is executed, all queued operations are sent to the database. flush can be called explicitly, or automatically executed before select query in order to be sure the select result reflects all previous operations

Grigori
04.07.2018
11:20:33
the code for deletion is just for md in md_list: md.delete()

I tried to force flush before and after each delete. No change.

Ok, I'll try to check it by calling SQL directly instead.

Alexander
04.07.2018
11:23:14
It is the first time I see "OperationalError: SQL logic error" exception

I think there is some logical problem inside the trigger

Grigori
04.07.2018
12:28:17
indeed, I was using some wrong syntax for triggers, which I copypasted from some moot source...

Thanks for the help!

Alexander
04.07.2018
12:28:34
Sure

Grigori
04.07.2018
12:33:25
by the way, would you recommend using database triggers, or Pony hooks for updating the state of FTS index tables in SQLite? What are the pros and cons for both approaches?

It would be very nice if Pony had an option for create_tables() to skip creating some tables. As it is now, we are forced to either create all tables through Pony, or through raw SQL.

Alexander
04.07.2018
12:53:11
Personally I'd use triggers for this specific task (maintaining up-to-date state of FTS tables). The benefit of trigger is it works always regardless do you change main table via Pony or with using raw SQL

Grigori
04.07.2018
12:54:19
you're right. But does Pony always plays safe with triggers?

Alexander
04.07.2018
12:56:35
When a trigger updates some column, and the object already loaded into memory within the current db_session, the object will not know about the attribute change, until you force its re-load. But for full text search it probably should not be an issue, because you typically update the main object and use FTS search in a two different db sessions

Grigori
04.07.2018
12:57:22
nice point

Alexander
04.07.2018
12:59:07
It would be very nice if Pony had an option for create_tables() to skip creating some tables. As it is now, we are forced to either create all tables through Pony, or through raw SQL.
You can create FTS tables using raw SQL and then perform db.generate_mapping(create_tables=True). Pony will create a table only if it doesn't already exists

It is possible to call db.execute before db.generate_mapping

Grigori
04.07.2018
13:02:25
Yeah, it worked. But I had to make a sandwich out of db.execute-generate_mapping-db.execute so triggers would see both tables...

Google
Grigori
04.07.2018
13:02:34
thanks again ))

Alexander
04.07.2018
13:11:02
Maybe we need to add possiblility to explicitly specify trigger text in Pony so the trigger will be created automatically at the right moment

Grigori
04.07.2018
15:18:11
it would be even better to add support for SQLite's virtual FTS tables.

stsouko
10.07.2018
08:34:34
Hello! Is this a bug? this works well left_join(mr.reaction for mr in db.MoleculeReaction if mr.molecule == molecule and mr.is_product == False)[:2] SELECT DISTINCT "reaction"."id", "reaction"."date", "reaction"."user", "reaction"."special" FROM "patents"."molecule_reaction" "mr" LEFT JOIN "patents"."reaction" "reaction" ON "mr"."reaction" = "reaction"."id" WHERE "mr"."molecule" = %(p1)s AND "mr"."is_product" = false LIMIT 2 {'p1':1} [Reaction[1], Reaction[1195]] but this: q = left_join(mr.reaction for mr in db.MoleculeReaction) q.filter(lambda mr: mr.molecule == molecule and mr.is_product == False)[:2] raise error: File "<string>", line 2, in filter File "/home/stsouko/Private/bydlocoding/CGRdb/env/lib/python3.5/site-packages/pony/utils/utils.py", line 76, in cut_traceback reraise(exc_type, exc, last_pony_tb) File "/home/stsouko/Private/bydlocoding/CGRdb/env/lib/python3.5/site-packages/pony/utils/utils.py", line 87, in reraise try: raise exc.with_traceback(tb) File "/home/stsouko/Private/bydlocoding/CGRdb/env/lib/python3.5/site-packages/pony/orm/sqltranslation.py", line 1553, in getattr 'Entity %s does not have attribute %s: {EXPR}' % (entity.__name__, name)) File "/home/stsouko/Private/bydlocoding/CGRdb/env/lib/python3.5/site-packages/pony/utils/utils.py", line 100, in throw raise exc # Set "pony.options.CUT_TRACEBACK = False" to see full traceback AttributeError: Entity Reaction does not have attribute molecule: mr.molecule

sorry!

I used filter not where

Jim
11.07.2018
16:26:42
HI, If EntityB inherits from entityA, is it possible to create create a new B but from the A. I tried item = A(classtype="B")but It seems not to work

Alexander
11.07.2018
16:27:29
Hi, I don't quite understand what do you mean

Jim
11.07.2018
16:30:41
class A(db.Entity): pass class B(A): passI want to do something like : item = A(classtype="B", **data)which should give the same thing as B(**data)

hope its clearer

Alexander
11.07.2018
16:37:17
I don't understand why do you want this, but this should work: from pony.orm import * db = Database('sqlite', ':memory:') class A(db.Entity): foo = Required(int) class B(A): bar = Optional(int) db.generate_mapping(create_tables=True) with db_session: x = A(classtype="B", foo=10) with db_session: objects = select(x for x in A)[:] print(objects) The result is [B[1]]

If you want to specify B's attributes as well, it will generate an error: >>> with db_session: ... x = A(classtype="B", foo=10, bar=20) ... Traceback (most recent call last): File "<input>", line 2, in <module> File "C:\dev\pony\pony\orm\core.py", line 4342, in __init__ if name not in entity._adict_: throw(TypeError, 'Unknown attribute %r' % name) File "C:\dev\pony\pony\utils\utils.py", line 129, in throw raise exc TypeError: Unknown attribute 'bar' But it looks logical

What is the problem you are trying to solve?

In Python it should be very easy to specify correct class at the object creation time

if you know a class name, you can do cls = db.entities[class_name] obj = cls(**data)

Jim
11.07.2018
17:12:06
ok thanks for the answer. since I have to use the attr of B this won't work. I will use model = getattr(db,classtype) instead.

Matthew
12.07.2018
11:41:44
Does anyone know of any Rust ORMs that have a similar design style to Pony?

Jim
13.07.2018
07:27:52
Hi. I have a 500MB csv file to load to databse via pony. I thought doing a commit every 1000 entries to not overload the RAM. does somebody has any experience of it ?

Matthew
13.07.2018
07:29:24
db_session(strict=True)

strict (bool) – when True the cache will be cleared on exiting the db_session. If you’ll try to access an object after the session is over, you’ll get the pony.orm.core.DatabaseSessionIsOver exception. Normally Pony strongly advises that you work with entity objects only within the db_session. But some Pony users want to access extracted objects in read-only mode even after the db_session is over. In order to provide this feature, by default, Pony doesn’t purge cache on exiting from the db_session. This might be handy, but in the same time, this can require more memory for keeping all objects extracted from the database in cache.

Google
Matthew
13.07.2018
07:29:58
and keep using new db_sessions every x records

that has worked for me in the past

Страница 64 из 75