[sqlalchemy] Re: Get the row count from a query

2022-06-04 Thread Sergey V.
An alternative would be session.query(User).filter(User.id_ == id).count() On Saturday, June 4, 2022 at 4:45:49 AM UTC+10 jason@stormfish-sci.com wrote: > I believe I finally found a solution: > > select(func.count(User.id_)).where(User.id_ == id) > > Thank you for taking the time to

[sqlalchemy] Re: SqlAlchemy with Postgres: How can I make a query that checks if a string is in a list inside a json column

2021-10-13 Thread Sergey V.
Use .any(): session.query(Gizmo).filter(Gizmo.users.any('user1')) On Wednesday, October 13, 2021 at 11:50:16 PM UTC+10 chat...@gmail.com wrote: > Imagine a Postgres JSON column with values like below: > "["user1", "user2"]" > > Is there any way to query a postgres JSON (not JSONB) column

[sqlalchemy] Re: Don't want foreign key on child to be updated when parent is updated

2019-02-26 Thread Sergey V.
This thing is called referential integrity (https://en.wikipedia.org/wiki/Referential_integrity) and is enforced on the database level - you can't have a value in Child.parent_id which is not in Parent.id. The ForeignKey creates a constraint in the database which ensures the referential

[sqlalchemy] Re: Dynamic table on the same declarative model

2019-02-03 Thread Sergey V.
We have a similar setup (a PostgreSQL schema per tenant), we deal with different database versions by running different *code versions* - i.e. if you need a new column you release a new code version which knows how to deal with the new column. Switching a tenant to that code version runs an

[sqlalchemy] Re: `func.similarity` performance

2016-03-31 Thread Sergey V.
Robert, my understanding is that SQLAlchemy knows nothing about the Postgres's `similarity` function - sqlalchemy.func just magically generates the SQL output depending on which member you invoke. Try `func.magic_unicorns()`. So, there's not much to optimize here - it outputs what you give it.

[sqlalchemy] How do I get an object from a relationship by object's PK?

2012-08-21 Thread Sergey V.
Hi all, I've asked this question on stackoverflow: http://stackoverflow.com/questions/12031861/sqlalchemy-how-do-i-get-an-object-from-a-relationship-by-objects-pk/12032405 Basically, I'm looking for a dict-like access to a relationship to be able to quickly retrieve items by some key (item's

[sqlalchemy] Re: Two self references, conflicting

2012-03-18 Thread Sergey V.
From your code it's not clear how you're going to differentiate between matches and merges - basically, you need something in your database which makes matches different from merges and then configure the retationships to use those fields. See the Boston addresses example in the documentation:

[sqlalchemy] Re: Database-side data mangling

2011-07-28 Thread Sergey V.
each time the attribute is accessed. Also, the _email attribute IS loaded and then sent back to the server, which sort of undermines the idea of database-side processing... On Jul 28, 2:06 pm, Michael Bayer mike...@zzzcomputing.com wrote: On Jul 27, 2011, at 8:56 PM, Sergey V. wrote: Good day

[sqlalchemy] Database-side data mangling

2011-07-27 Thread Sergey V.
Good day, I'm trying to figure out how to do something similar to the Symmetric Encryption recipe (http://www.sqlalchemy.org/trac/wiki/UsageRecipes/ SymmetricEncryption), only on the database side, not in Python. I have a suspicion that @compiles decorator may provide a solution, but having

[sqlalchemy] Re: confused on avoiding sql injections using ORM

2011-07-05 Thread Sergey V.
Say I create an instance of a mapped class and then attach some values to it. And want to do session.add. If you're worried about something like this: user = User() user.name = ;DROP TABLE users; session.add(user) then don't be, there is no possibility of SQL injection here,

[sqlalchemy] Re: confused on avoiding sql injections using ORM

2011-07-04 Thread Sergey V.
Can you give an example of sql injection working with ORM? Some sample code etc. On Jul 5, 5:41 am, Krishnakant Mane krm...@gmail.com wrote: Hello all. I use Pylons 0.9.7 and sqlalchemy. I use the Object Relational Mapper with declarative syntax in a few of my modules. I was reading chapter

[sqlalchemy] Re: Speed matters

2011-06-01 Thread Sergey V.
Hi, One easy/obvious improvement would be to delete all user's identifiers and groups at once without iterating over them for every user. Actually, iterating over the list of user_ids is not necessary too I think. code session.query(Identifier).filter(Identifier.user_id.in_(user_ids)).delete()

[sqlalchemy] Re: Setting column value that changes everyday

2011-03-01 Thread Sergey V.
Do you need to store expiry_code? seeing as it is a function of last_con and the current date. Second that. I would also point out that phone number probably shouldn't be an integer - how would you store phone numbers which start with 0, for example? I'd rather make it a String. -- You

[sqlalchemy] Re: Odd many-to-one problem

2010-11-11 Thread Sergey V.
relationship() expects a class or a mapper instance, not a string. I got this error: ArgumentError: relationship 'available_deals' expects a class or a mapper argument (received: type 'str') Hmm... I'm not sure what I'm doing wrong but passing strings to relation() definitely works for me:

[sqlalchemy] Re: Odd many-to-one problem

2010-11-10 Thread Sergey V.
The twist is that I've spread out my tables and ORM classes across several files. I've tried to keep it so that I don't have circular dependencies. That means I've defined Merchant first, and then Deal later, in separate files To avoid problems with imports and dependencies you can pass

[sqlalchemy] association_proxy question... I think

2009-12-02 Thread Sergey V.
Hi all, I've got two tables: Users (id, password) and UserProperties (user_id, name, value). Is there a way to map the properties stored in UserProperties as attributes of User object? I mean, john = User('john', 'password') john.name = John Smith # creates UserProperty('john', 'name', 'John

[sqlalchemy] Check if an item exists in a relation

2009-10-29 Thread Sergey V.
Hi all, I must be missing something obvious here... Let's suppose I have the following class: class User(Base): # addresses = relation(Address, backref=user) and I have a number which may be an ID of an Address object. How do I check if the number is an ID of one of Addresses of

[sqlalchemy] Re: Check if an item exists in a relation

2009-10-29 Thread Sergey V.
Some assumptions: 1. SA-mapped object means the user object in the example 2. property name means addresses in the example 3. The function shouldn't assume that you want an Address object 4. The ID attribute is known ahead of time (e.g. its always id). If not, your function will need

[sqlalchemy] Re: Check if an item exists in a relation

2009-10-29 Thread Sergey V.
Ahh... I missed the relation.any() part of your example - with it the code should behave exactly as I need. I think. I need to give it a try. Thanks! On Oct 30, 9:53 am, Sergey V. sergey.volob...@gmail.com wrote: Some assumptions: 1. SA-mapped object means the user object in the example 2