peewee

Avoiding conflicting column titles in table join in peewee

房东的猫 提交于 2019-12-12 11:18:18
问题 I'm trying to join two tables in peewee with a mysql database. This is pretty easy doing something like this: s = Table1.select(Table1, Table2).join( Table2).naive().where(Table1.Title == "whatever") Unfortunately, I have called a column in Table1 and Table2 the same thing, "URL". Then when I select s.URL it gives me the URL from Table2, which I don't want, I want the one from Table1. Is there some way to either not join the Table2.URL column or to name it something different? This question

Peewee says “cannot commit - no transaction is active”

痴心易碎 提交于 2019-12-12 02:45:43
问题 My CherryPy app does some cleaning every hour with the following code: def every_hour(): two_hours_ago = time.time() - 2 * 60 * 60 DbChoice.delete().where(DbChoice.time_stamp < two_hours_ago).execute() monitor_every_hour = Monitor(cherrypy.engine, every_hour, frequency=60 * 60) monitor_every_hour.start() Sometimes it crashes with he following message: Traceback (most recent call last): File "C:\Python34\lib\site-packages\peewee.py", line 2364, in execute_sql self.commit() File "C:\Python34

Cherrypy + sqlite3 + Peewee crashes when two processes execute the same code at the same time

为君一笑 提交于 2019-12-11 18:57:56
问题 Navigating to the page test?x=a defined below it works. Navigating to test?x=a and then quickly navigating to test?x=b , both the cycles will keep running for a few seconds, but one of them will eventually crash with the error peewee.OperationalError: cannot start a transaction within a transaction . This is obviously not a real world test, it is a way to reproduce the real world problems that I am having once in a while. In the real world application I see errors similar to this one when

Perform a substring query in Peewee

若如初见. 提交于 2019-12-11 10:48:10
问题 I'm using Python 2.7 together with Peewee. At this moment, I need to use Peewee to execute the following SQL query: select a, b, substring(c, 1, 3) as alias1, count(substring(c, 1, 3)) as alias2 from Table where <some where condition, not relevant here> group by a, alias1 My first problem here is how to perform a substring with Peewee. I have searched the documentation and, so far, no lucky. So, the basic question is: How do I perform a substring SQL function using Peewee? It would also be

Update existing table/model column/fields?

好久不见. 提交于 2019-12-11 08:07:26
问题 How can I update a tables columns and column data types in PeeWee? I have already created the table Person in the database from my model. But I've now added some new fields to the model and changed the type of certain existing fields/columns. The following doesn't update the table structure: psql_db = PostgresqlExtDatabase( 'MyDB', user='foo', password='bar', host='', port='5432', register_hstore=False ) class PsqlModel(Model): """A base model that will use our Postgresql database""" class

Remove duplicate entries in peewee

久未见 提交于 2019-12-11 04:14:30
问题 I have a quick function that I threw up together to remove duplicates on my table given a particular combination of fields: for l in table.select(): if table.select().where((table.Field1==l.Field1) & (table.Field2==l.Field2) & ....).count()>1: l.delete() l.save() But I imagine that there's a better way to do this 回答1: You could add a unique constraint on the columns you wish to be unique, then let the database enforce the rules for you. That'd be the best way. For peewee, that looks like:

Peewee - How to Convert a Dict into a Model

南笙酒味 提交于 2019-12-11 01:28:43
问题 Lets say I have import peewee class Foo(Model): name = CharField() I would like to do the following: f = {id:1, name:"bar"} foo = Foo.create_from_dict(f) Is this native in Peewee? I was unable to spot anything in the source code. I've wrote this function which works but would rather use the native function if it exists: #clazz is a string for the name of the Model, i.e. 'Foo' def model_from_dict(clazz, dictionary): #convert the string into the actual model class clazz = reduce(getattr, clazz

limit number of rows peewee retrieve

半世苍凉 提交于 2019-12-11 00:14:45
问题 I have the following python source file to retrieve rows from MySQL database. The problem is there are too many rows to retrieve in subscriber table. How can I do limit to small chunk of rows to retrieve and process and so on? class subinfo(peewee.Model): sub_id = peewee.IntegerField() active = peewee.BooleanField() sub_type = peewee.IntegerField() sub_cat = peewee.TextField() class Meta: database = locdb class subscriber(peewee.Model): sub_id = peewee.IntegerField(unique=True, primary_key

peewee - Define models separately from Database() initialization

断了今生、忘了曾经 提交于 2019-12-10 23:07:21
问题 I need to use some ORM engine, like peewee , for handling SQLite database within my python application. However, most of such libraries offer syntax like this to define models.py : import peewee db = peewee.Database('hello.sqlite') class Person(peewee.Model): name = peewee.CharField() class Meta: database = db However, in my application, i cannot use such syntax since database file name is provided by outside code after import, from module, which imports my models.py . How to initialize

Is it possible to make sql join on several fields using peewee python ORM?

核能气质少年 提交于 2019-12-10 16:48:42
问题 Assuming we have these three models. class Item(BaseModel): title = CharField() class User(BaseModel): name = CharField() class UserAnswer(BaseModel): user = ForeignKeyField(User, 'user_answers') item = ForeignKeyField(Item, 'user_answers_items') answer = ForeignKeyField(Item, 'user_answers') I want to get all Items which does not have related UserAnswer records for current user. In SQL it would be something like this: select * from item i left join useranswer ua on ua.item_id=i.id and ua