Jacob
;)
Lucky
I ditched pizza an got chickennuggets.
Jacob
Also, is there a way to implicitly add a condition to every query for a given model? By default (or even a hard coded unconditional addition to every SELECT), I only want to select entities that have model.deleted=="N" (the stupid pseudo Boolean column).
Lucky
Jacob
But then I still need to remember to use the lambda var...
Jacob
I can look into views, no one else is using them currently, so maybe I can add them without affecting other apps using the DB. Hmm...
Lucky
How do I use the result of them in the same file?
The problem is basically we can only access the table Entities from the same file where the `db=Database()` part is.
And the database binding will be at the users side, as he gets to choose the database used. Mysql, posgres,...
That means that the entities from the library aren't available inside the library to use, as i cannot import the db from the users code, that is independent
Jacob
I've been toying with the idea of using punq to do light weight dependency injection, injecting the db somehow...
Lucky
I think the idea of blueprints is all that is needed.
Basically we’d have to change the EntityMeta to not directly act, but use a mehod call on the db to register it’s entity.
That way the blueprints store that call.
Basically flask Blueprints are really easy.
You have a deferred_functions list, and every function from the Flask object get replaced by a call to record_once, which just adds that to the deferred_functions list.
Lucky
As soon as you call blueprint.register(app), it will iterate though that list and call them functions
Alexander
In new migrations you will be able to work with a new layer of entites (as we call it now VirtualEntities) and it gives you an ability to dynamically define entities and even attributes.
Jacob
WOW! That sounds good! Is that what's in the orm-migrations branch?
Jacob
I'd love to see more about VirtualEntities :)
Jacob
Is there any way in the online editor to have a relationship with an entity that has a composite primary key? (I need to specify that two columns must match, not just one)
Alexander
No, it's not published yet. It is in dev branch.
Sigmund
Out of curiosity, what databases are people here using?
Matthew
Mainly Postgres, with some SQLite
Sigmund
Yeah, I've noticed that Postgres seems to be popular. Whenever I see a talk/presentation that involves a relational database, it's Postgres more often than not.
Sigmund
I imagine that SQLite is used a lot for testing
Matthew
Yep
Matthew
SQLite is also useful when having a single file rather than an independent database is a benefit
Matthew
If you are new to databases and are using pony, any popular one is fine
Sigmund
I'm not new to databases, but would like to use PonyORM next time I have a project involving a relational database. I've spent quite a bit of time reading about database comparisons, and to me it looks like the best way is just to pick one, then worry about performance etc. later if needs be.
Matthew
I agree
Vitaliy
I use both Postges and SQLite in production. The latter is used to store historical and some other data. But Pony does not allow for entities backed by different databases to be interlinked with each other. I hope it will be implemented sometime 🙂
Matthew
I have a timezone offset from an external system which is supplied to me as a float. Pony doesn’t seem to support a float for timedelta eg timedelta(hours=5.5)
Matthew
Is that a bug or intended behaviour?
Matthew
TypeError: 'hours' argument of timedelta(...) function must be of 'int' type. Got: 'float' (inside X.y)
Matthew
Should I just do timedelta(seconds=value * 3600) ?
Matthew
I am generating the timedelta iinside a method
Matthew
calling the method directly works
Matthew
using the method within a pony query causes issues
Matthew
seems like it may be a bug
Matthew
def current_time(self):
return datetime.datetime.utcnow() + datetime.timedelta(seconds=int(self.y *
3600))
Matthew
a query like select(.... if x.z < a.current_time()) blows up
Matthew
pony==0.7.9
Alexander
Error is?
Matthew
AssertionError: [CallFunc(Name('int'), [Mul((Getattr(Name('self'), 'facebook_timezone_offset_hours_utc'), Const(3600)))], None, None), CallFunc(Name('int'), [Mul((Getattr(Name('self'), 'facebook_timezone_offset_hours_utc'), Const(3600)))], None, None), AssName('seconds', 'OP_ASSIGN'), AssName('seconds', 'OP_ASSIGN'), CallFunc(Name('print'), [Name('seconds')], None, None), CallFunc(Getattr(Name('datetime'), 'timedelta'), [Name('seconds')], None, None), AssName('delta', 'OP_ASSIGN'), CallFunc(Name('print'), [Name('delta')], None, None)]
Matthew
It seems like the query translator gets confused
Alexander
And what is the line of exception inside pony? (before throw() method)
Jacob
Why is the dev branch in a private repo? Is that an artifact of how Pony used to be dual licensed? (is it still dual licensed to people that originally bought the license requiring you to keep a separate repo?)
Matthew
What's the best way to add a string constant to a pony model?
Matthew
class X(db.Entity):
...
my_constant = 'blah'
Matthew
?
Alexander
yes
Matthew
thank you
Carel
Hi all, I’m working on a script where I need to create entities on the fly, that is I do not know the full scheme before hand, is it possible to do this in pony ?
Alexander
It is possible, but not very convenient. After migrations release it will be easier. Right now you can do it using metaclasses:
Alexander
In simple cases (if you don't use composite keys) you can do something like that:
from pony import orm
from pony.orm import core
def define_entity(db, entity_name, attrs):
return core.EntityMeta(entity_name, (db.Entity,), attrs)
db = orm.Database()
entity = define_entity(db, 'Person', dict(
name=orm.Required(str),
age=orm.Optional(int)
))
orm.sql_debug(True)
db.bind('sqlite', ':memory:')
db.generate_mapping(create_tables=True)
Carel
Thanks, I’ll try it out, should fit quite well with what I’m doing :)
Jeff
Hello sweet people of the Pony community
Alexey
Lucky
Иван
Hi, i have a question
I use pony orm with sqlite in wal mode
I create two different orm.db_session to work with
In first transaction , i have deleted all rows from some table
In second transaction , I'm trying to read count of rows in this table. And i have got 0 not as expected for me .The trouble is the fact that when i'm trying to read in transaction2, the deletions from transaction1 have not already committed, so read operation from transaction2 should return not 0, because rows exists in this table
What may be the case of this trouble?
I also have tried to use embedded in python sqlite3 library instead of second pony transaction. And with this library all is fine - i have got correct count of rows
What's wrong?
Иван
It is not nested transaction
It's just two different transactions
Иван
I have written some code of this
https://pastebin.com/TueTriqC
Иван
In logs we will see "table has 0 rows ", but first transaction was not committed
Иван
Seems like second transaction running inside of first.. but why and how to deal with it?
Jacob
I don't think you can use await in a db_session context manager, as it is not an asynchronous context manager, so it commits as soon as you await because you exited the context.
Jacob
You can decorate an async function however according the docs (search for coroutine)
Jacob
With it decorated, you have to explicitly commit when you're ready
Иван
Alexander
Database transactions is not so good together with asynchronous code
Jacob
I'm beginning a project that uses aiohttp in serving a web API with pony as the interface for a DB. This article convinced me this could work. It went into some of the problems of trying to use await in the middle of a DB transaction. https://techspot.zzzeek.org/2015/02/15/asynchronous-python-and-databases/
Lucky
Hey there.
I'm getting ValueError: Attribute State.chat_id is required while looking up an value,
failing the State.get(...) call, and I did provide the chat_id as argument:
class State(db.Entity, self.State):
user_id = orm.Required(int, nullable=True) # can be None (e.g. channels)
chat_id = orm.Required(int, nullable=True) # can be None (e.g. inline_query)
state = orm.Required(str)
data = orm.Optional(orm.Json, nullable=True) # can be None
orm.PrimaryKey(user_id, chat_id)
# end class
user_id=10717954
chat_id=None
db_state = self.StateTable.get(chat_id=chat_id, user_id=user_id)
Lucky
What I think is strange is that it is happening on read access.
Edit: also happening on creation.
Lucky
Setting is_volatile=True fixed it.
Lucky
I created a Pull Request #444 for fixing that.
Lucky
In the editor.ponyorm is the autosave enabled again?
Lucky
That’s really bad for my workflow.
I often make minor changes, quite quickly until I get what I have envisioned, which I don’t want to snapshot.
At the end of that I get a version which I copy in my project, to have under version control. Usually I don’t remember to open up the editor one more time just to save a snapshot. If there has passed quite some time and I open the editor up again, that most of the time implied that the model there was worth a snapshot, but I opened up the editor, to make changes I need now, and that’s what I gonna do, without thinking about doing a snapshot of the old version before changes.
Here is how it typically goes:
- Open existing schema
- Make changes
- Remember „Oh, I should have made a snapshot of the old version“
- Open same diagram in new tab
- Create Snapshot of version before
- Close Tab
- Save (overwrite) the version with just made changes.
Obviously, with autosave it’s not possible to get back to that earlier version.
Lucky
Maybe you could keep autosave (I.e. browser crashed, halp) and manual save (I.e. I’m happy with the diagram) two separate things?
Lucky
Lucky
Can confirm, it is saving without my consent when I add/remove entitites, making my previous changes forever lost
Lucky
:(
Lucky
I accidentially removed an entity.
Lucky
Now it’s gone
Lucky
😭
Alexander
Hi, what do you think would be correct UI behavior?
Lucky
Don’t save unless I press save?