Re: [sqlalchemy] How do I get an object and update multiple fields in sqlalchemy
I did as below. But didn't work. Am i missing anything else. def update_record(session, id): record_instance = get_instance(id) if record_instance: updateobj(record_instance, {'time_zone':'GMT','status':'done'}) session.flush() session.commit() def updateobj(obj, data): for key, value in data.items(): setattr(obj, key, value) On Friday, 18 August 2017 19:47:50 UTC+5:30, Simon King wrote: > > No, but you can trivially write your own function to do it: > > def updateobj(obj, data): > for key, value in data.items(): > setattr(obj, key, value) > > Simon > > On Fri, Aug 18, 2017 at 3:14 PM, pravin battula > <pravin@gmail.com > wrote: > > The solution which you gave will work but I have a dict of keys to be > > updated with that get instance. Is there any specific way of updating > > something like product_live_time_instance.update(data_dict). > > > > > > On Friday, 18 August 2017 19:21:12 UTC+5:30, Simon King wrote: > >> > >> On Fri, Aug 18, 2017 at 2:41 PM, pravin battula > >> <pravin@gmail.com> wrote: > >> > Hi, > >> > > >> > I'm getting an instance of a model using a primary key like below. > >> > product_live_time = session.query(ProductLiveTime).get(product_id) > >> > > >> > Now, i want to update few columns using the same instance > >> > product_live_time, > >> > how can i do it without doing filter again like below i.e > >> > session.query(ProductLiveTime).filter(ProductLiveTime.product_id == > >> > product_id).update(data). > >> > is there any other better way of doing it. > >> > > >> > >> Maybe I'm misunderstanding the question, but: > >> > >> product_live_item.attr1 = value1 > >> product_live_item.attr2 = value2 > >> session.flush() > >> > >> Is that what you meant? > >> > >> Simon > > > > -- > > SQLAlchemy - > > The Python SQL Toolkit and Object Relational Mapper > > > > http://www.sqlalchemy.org/ > > > > To post example code, please provide an MCVE: Minimal, Complete, and > > Verifiable Example. See http://stackoverflow.com/help/mcve for a full > > description. > > --- > > You received this message because you are subscribed to the Google > Groups > > "sqlalchemy" group. > > To unsubscribe from this group and stop receiving emails from it, send > an > > email to sqlalchemy+...@googlegroups.com . > > To post to this group, send email to sqlal...@googlegroups.com > . > > Visit this group at https://groups.google.com/group/sqlalchemy. > > For more options, visit https://groups.google.com/d/optout. > -- SQLAlchemy - The Python SQL Toolkit and Object Relational Mapper http://www.sqlalchemy.org/ To post example code, please provide an MCVE: Minimal, Complete, and Verifiable Example. See http://stackoverflow.com/help/mcve for a full description. --- You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+unsubscr...@googlegroups.com. To post to this group, send email to sqlalchemy@googlegroups.com. Visit this group at https://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout.
Re: [sqlalchemy] How do I get an object and update multiple fields in sqlalchemy
The solution which you gave will work but I have a dict of keys to be updated with that get instance. Is there any specific way of updating something like product_live_time_instance.update(data_dict). On Friday, 18 August 2017 19:21:12 UTC+5:30, Simon King wrote: > > On Fri, Aug 18, 2017 at 2:41 PM, pravin battula > <pravin@gmail.com > wrote: > > Hi, > > > > I'm getting an instance of a model using a primary key like below. > > product_live_time = session.query(ProductLiveTime).get(product_id) > > > > Now, i want to update few columns using the same instance > product_live_time, > > how can i do it without doing filter again like below i.e > > session.query(ProductLiveTime).filter(ProductLiveTime.product_id == > > product_id).update(data). > > is there any other better way of doing it. > > > > Maybe I'm misunderstanding the question, but: > > product_live_item.attr1 = value1 > product_live_item.attr2 = value2 > session.flush() > > Is that what you meant? > > Simon > -- SQLAlchemy - The Python SQL Toolkit and Object Relational Mapper http://www.sqlalchemy.org/ To post example code, please provide an MCVE: Minimal, Complete, and Verifiable Example. See http://stackoverflow.com/help/mcve for a full description. --- You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+unsubscr...@googlegroups.com. To post to this group, send email to sqlalchemy@googlegroups.com. Visit this group at https://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout.
[sqlalchemy] How do I get an object and update multiple fields in sqlalchemy
Hi, I'm getting an instance of a model using a primary key like below. *product_live_time = session.query(ProductLiveTime).get(product_id)* Now, i want to update few columns using the same instance *product_live_time, *how can i do it without doing filter again like below i.e session.query(ProductLiveTime).filter(ProductLiveTime.product_id == product_id).update(data). is there any other better way of doing it. -- SQLAlchemy - The Python SQL Toolkit and Object Relational Mapper http://www.sqlalchemy.org/ To post example code, please provide an MCVE: Minimal, Complete, and Verifiable Example. See http://stackoverflow.com/help/mcve for a full description. --- You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+unsubscr...@googlegroups.com. To post to this group, send email to sqlalchemy@googlegroups.com. Visit this group at https://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout.
[sqlalchemy] Not able to pass Non-ASCII strings as a filter to raw sql select statement
Hi, When i pass a Non-ASCII string in where condition filter of an raw SQL statement, the SQLALCHEMY showing below error during an execution of query File /base/data/home/apps/s~ipaddress/versionnumber/flow/services/requestExport.py, line 119, in getRecordsFromDB instanceList = self.session.raw_table_join_query(metadata, baseQuery, columns, orderBy = args['orderBy'], isDescending = args['isDescending'], limit=5000, includeAttachments=args.get('includeAttachments')) File /base/data/home/apps/s~ipaddress/versionnumber/orangescape/model/server/sql/SQLSession.py, line 724, in raw_table_join_query rs = self.getConnection().execute(sql) File /base/data/home/apps/s~ipaddress/versionnumber/sqlalchemy/engine/base.py, line 1294, in execute params) File /base/data/home/apps/s~ipaddress/versionnumber/sqlalchemy/engine/base.py, line 1471, in _execute_text statement, parameters File /base/data/home/apps/s~ipaddress/versionnumber/sqlalchemy/engine/base.py, line 1528, in _execute_context context) File /base/data/home/apps/s~ipaddress/versionnumber/sqlalchemy/engine/default.py, line 325, in do_execute cursor.execute(statement, parameters) File /base/data/home/runtimes/python27/python27_lib/versions/1/google/storage/speckle/python/api/rdbms.py, line 565, in execute request.statement = _ConvertFormatToQmark(statement, args) File /base/data/home/runtimes/python27/python27_lib/versions/1/google/net/proto2/python/internal/python_message.py, line 449, in setter self._fields[field] = type_checker.CheckValue(new_value) File /base/data/home/runtimes/python27/python27_lib/versions/1/google/net/proto2/python/internal/type_checkers.py, line 151, in CheckValue (proposed_value)) *ValueError: SELECT po.id FROM purchaseorder as po where po.customername = 'Twójstarski' t has type bytes, but isn't in 7-bit ASCII encoding. Non-ASCII strings must be converted to unicode objects before being added.* The below is the query i'm using *SELECT po.id FROM purchaseorder as po where po.customername = 'Twójstarski'* I tried passing the value has an unicode string, this time the query getting executed properly but without any result set. Please suggest me the way to deal with this Non-ASCII string. Regards Pravin B -- You received this message because you are subscribed to the Google Groups sqlalchemy group. To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+unsubscr...@googlegroups.com. To post to this group, send email to sqlalchemy@googlegroups.com. Visit this group at http://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout.
[sqlalchemy] Renaming primary key column in MYSQL
Hi, I'm getting below error while trying to rename a primary key column. *OperationalError: (OperationalError) (1025, Error on rename of '.\\test\\#sql-540_8' to '.\\test\\users' (errno: 150)) '\nALTER TABLE users CHANGE COLUMN user_id `Users_Id` INTEGER NOT NULL AUTO_INCREMENT' () * Below is the code i used to alter the column. *alter_column(users.c.user_id, name='Users_Id')* * * Please do the needful Regards Pravin B -- You received this message because you are subscribed to the Google Groups sqlalchemy group. To view this discussion on the web visit https://groups.google.com/d/msg/sqlalchemy/-/AQ0TELALHowJ. To post to this group, send email to sqlalchemy@googlegroups.com. To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.
[sqlalchemy] Re: Renaming primary key column in MYSQL
Hey Ergo, Thanks for the reply. I wonder what will happen to the performance when trying to drop and recreate the foreign key constraints if the table has thousands of data(lets say 50k)? Regards Pravin B On Monday, 23 July 2012 19:02:23 UTC+5:30, Ergo wrote: Hi Pravin, The problem you are seeing here is probably related to the fact you have some other foreign keys that rely on the user_id column, Mysql will do everything to make your life miserable if you change the name of the column/size of type that has other constraints depend on it - and the error message is not exactly helpful here. To fix your issue you will have to first drop the other constraints that depend on the column, then rename it, and then recreate constraints again. -- You received this message because you are subscribed to the Google Groups sqlalchemy group. To view this discussion on the web visit https://groups.google.com/d/msg/sqlalchemy/-/N4Y43tZR3LEJ. To post to this group, send email to sqlalchemy@googlegroups.com. To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.
[sqlalchemy] Re: Bulk creation of columns
You need to create new Column objects for each table because the table objects take ownership of the passed in columns. I think that is what i'm doing,creating new column objects for 2 columns and first appending it to the table object and then trying to create those columns using create_column,please correct me if i'm wrong. On 21 Sep, 20:24, AM age...@themactionfaction.com wrote: On Sep 21, 2011, at 6:30 AM, pravin battula wrote: Hi, How can i create columns in bulk? I tried as below but doesn't work. migrate_engine = create_engine('mysql://root:root@localhost/ payroll', echo=False) metadata = MetaData(bind = migrate_engine) metadata.reflect(bind = migrate_engine, schema = 'payroll') tableObj = metadata.tables.get('test.salary') colList = [Column('description',String(100)),Column('information',String(50))] tableObj.append_column(colList) tableObj.create_column(colList) You need to create new Column objects for each table because the table objects take ownership of the passed in columns. Cheers, M -- You received this message because you are subscribed to the Google Groups sqlalchemy group. To post to this group, send email to sqlalchemy@googlegroups.com. To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.
[sqlalchemy] Bulk creation of columns
Hi, How can i create columns in bulk? I tried as below but doesn't work. migrate_engine = create_engine('mysql://root:root@localhost/ payroll', echo=False) metadata = MetaData(bind = migrate_engine) metadata.reflect(bind = migrate_engine, schema = 'payroll') tableObj = metadata.tables.get('test.salary') colList = [Column('description',String(100)),Column('information',String(50))] tableObj.append_column(colList) tableObj.create_column(colList) Please do the needful. -- You received this message because you are subscribed to the Google Groups sqlalchemy group. To post to this group, send email to sqlalchemy@googlegroups.com. To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.
[sqlalchemy] Re: Bulk creation of columns
i'm getting the below error. 'list' object has no attribute '_set_parent_with_dispatch' On Sep 21, 3:30 pm, pravin battula pravin.batt...@gmail.com wrote: Hi, How can i create columns in bulk? I tried as below but doesn't work. migrate_engine = create_engine('mysql://root:root@localhost/ payroll', echo=False) metadata = MetaData(bind = migrate_engine) metadata.reflect(bind = migrate_engine, schema = 'payroll') tableObj = metadata.tables.get('test.salary') colList = [Column('description',String(100)),Column('information',String(50))] tableObj.append_column(colList) tableObj.create_column(colList) Please do the needful. -- You received this message because you are subscribed to the Google Groups sqlalchemy group. To post to this group, send email to sqlalchemy@googlegroups.com. To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.
[sqlalchemy] Bulk creation of columns
Hi, How can i create columns in bulk using create_column method? I tried as below,. migrate_engine = create_engine('mysql://root:root@localhost/ payroll', echo=False) metadata = MetaData(bind = migrate_engine) metadata.reflect(bind = migrate_engine, schema = 'payroll') tableObj = metadata.tables.get('test.salary') colList = [Column('description',String(100)),Column('information',String(50))] tableObj.append_column(*colList) tableObj.create_column(*colList) getting an error as *TypeError*:*create() got multiple values for keyword argument 'table'* Please do the needful. -- You received this message because you are subscribed to the Google Groups sqlalchemy group. To view this discussion on the web visit https://groups.google.com/d/msg/sqlalchemy/-/l6P5vE-GBUwJ. To post to this group, send email to sqlalchemy@googlegroups.com. To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.
[sqlalchemy] Error while using CAST
Hi, I'm using cast to update values in a table by issuing following command. table.update().values(empno = cast(empno,Integer)).execute(). Where as empno is an string field,i'm trying to convert the data from empno column from string to integer and then issuing the below command to alter the data type of the column by issuing following command. alter_column(table.c.empno,type=Integer). It shows an error as OperationalError: (OperationalError) (1292, Truncated incorrect INTEGER value: '1d') 'UPDATE test.mytable SET `newColumn`=CAST(CAST(%s AS CHAR) AS SIGNED INTEGER)' ('1d',) Please do the needful -- You received this message because you are subscribed to the Google Groups sqlalchemy group. To post to this group, send email to sqlalchemy@googlegroups.com. To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.
[sqlalchemy] Re: Error while using CAST
Sorry for the spelling mistake.It shows an error as below. OperationalError: (OperationalError) (1292, Truncated incorrect INTEGER value: 'testing') 'UPDATE test.mytable SET `newColumn`=CAST(test.mytable.`empno` AS SIGNED INTEGER)' () On Sep 14, 6:48 pm, pravin battula pravin.batt...@gmail.com wrote: Hi, I'm using cast to update values in a table by issuing following command. table.update().values(empno = cast(empno,Integer)).execute(). Where as empno is an string field,i'm trying to convert the data from empno column from string to integer and then issuing the below command to alter the data type of the column by issuing following command. alter_column(table.c.empno,type=Integer). It shows an error as OperationalError: (OperationalError) (1292, Truncated incorrect INTEGER value: '1d') 'UPDATE test.mytable SET `newColumn`=CAST(CAST(%s AS CHAR) AS SIGNED INTEGER)' ('1d',) Please do the needful -- You received this message because you are subscribed to the Google Groups sqlalchemy group. To post to this group, send email to sqlalchemy@googlegroups.com. To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.
[sqlalchemy] Re: Error while using CAST
Hi Mike, I'm using Mysql 5.0 backend On Sep 14, 8:20 pm, Mike Conley mconl...@gmail.com wrote: Don't know what database you are using, but this looks like you are trying to cast the string 'testing' to an integer and the database engine says you can't do that. -- Mike Conley On Wed, Sep 14, 2011 at 9:51 AM, pravin battula pravin.batt...@gmail.comwrote: Sorry for the spelling mistake.It shows an error as below. OperationalError: (OperationalError) (1292, Truncated incorrect INTEGER value: 'testing') 'UPDATE test.mytable SET `newColumn`=CAST(test.mytable.`empno` AS SIGNED INTEGER)' () On Sep 14, 6:48 pm, pravin battula pravin.batt...@gmail.com wrote: Hi, I'm using cast to update values in a table by issuing following command. table.update().values(empno = cast(empno,Integer)).execute(). Where as empno is an string field,i'm trying to convert the data from empno column from string to integer and then issuing the below command to alter the data type of the column by issuing following command. alter_column(table.c.empno,type=Integer). It shows an error as OperationalError: (OperationalError) (1292, Truncated incorrect INTEGER value: '1d') 'UPDATE test.mytable SET `newColumn`=CAST(CAST(%s AS CHAR) AS SIGNED INTEGER)' ('1d',) Please do the needful -- You received this message because you are subscribed to the Google Groups sqlalchemy group. To post to this group, send email to sqlalchemy@googlegroups.com. To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en. -- You received this message because you are subscribed to the Google Groups sqlalchemy group. To post to this group, send email to sqlalchemy@googlegroups.com. To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.
[sqlalchemy] Re: Error while using CAST
Mike, when i execute the below sql statement directly in the database using sqlyog,it works fine but when tried with sqlalchemy it didn't. update mytable set EmpMaster = cast(empno as UNSIGNED INTEGER) On Sep 14, 8:23 pm, pravin battula pravin.batt...@gmail.com wrote: Hi Mike, I'm using Mysql 5.0 backend On Sep 14, 8:20 pm, Mike Conley mconl...@gmail.com wrote: Don't know what database you are using, but this looks like you are trying to cast the string 'testing' to an integer and the database engine says you can't do that. -- Mike Conley On Wed, Sep 14, 2011 at 9:51 AM, pravin battula pravin.batt...@gmail.comwrote: Sorry for the spelling mistake.It shows an error as below. OperationalError: (OperationalError) (1292, Truncated incorrect INTEGER value: 'testing') 'UPDATE test.mytable SET `newColumn`=CAST(test.mytable.`empno` AS SIGNED INTEGER)' () On Sep 14, 6:48 pm, pravin battula pravin.batt...@gmail.com wrote: Hi, I'm using cast to update values in a table by issuing following command. table.update().values(empno = cast(empno,Integer)).execute(). Where as empno is an string field,i'm trying to convert the data from empno column from string to integer and then issuing the below command to alter the data type of the column by issuing following command. alter_column(table.c.empno,type=Integer). It shows an error as OperationalError: (OperationalError) (1292, Truncated incorrect INTEGER value: '1d') 'UPDATE test.mytable SET `newColumn`=CAST(CAST(%s AS CHAR) AS SIGNED INTEGER)' ('1d',) Please do the needful -- You received this message because you are subscribed to the Google Groups sqlalchemy group. To post to this group, send email to sqlalchemy@googlegroups.com. To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en. -- You received this message because you are subscribed to the Google Groups sqlalchemy group. To post to this group, send email to sqlalchemy@googlegroups.com. To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.
[sqlalchemy] Re: Error while using CAST
Hi King, Thanks for the reply,I tried giving table.update().values(empno = cast(table.c.empno,Integer)).execute() but still the same the same error. OperationalError: (OperationalError) (1292, Truncated incorrect INTEGER value: 'testing') 'UPDATE test.mytable SET `empno`=CAST(test.mytable.`empno` AS SIGNED INTEGER)' () Regards Pravin B On Sep 14, 8:38 pm, King Simon-NFHD78 simon.k...@motorolasolutions.com wrote: Does this work instead: table.update().values(empno = cast(table.c.empno,Integer)).execute() ie. a bare 'empno' inside your cast expression is just referring to a python variable 'empno', which you've probably set to the value 'testing' at some other point in your code. You need the column object table.c.empno instead Hope that helps, Simon pravin battula wrote Mike, when i execute the below sql statement directly in the database using sqlyog,it works fine but when tried with sqlalchemy it didn't. update mytable set EmpMaster = cast(empno as UNSIGNED INTEGER) On Sep 14, 8:23 pm, pravin battula pravin.batt...@gmail.com wrote: Hi Mike, I'm using Mysql 5.0 backend On Sep 14, 8:20 pm, Mike Conley mconl...@gmail.com wrote: Don't know what database you are using, but this looks like you are trying to cast the string 'testing' to an integer and the database engine says you can't do that. -- Mike Conley On Wed, Sep 14, 2011 at 9:51 AM, pravin battula pravin.batt...@gmail.comwrote: Sorry for the spelling mistake.It shows an error as below. OperationalError: (OperationalError) (1292, Truncated incorrect INTEGER value: 'testing') 'UPDATE test.mytable SET `newColumn`=CAST(test.mytable.`empno` AS SIGNED INTEGER)' () On Sep 14, 6:48 pm, pravin battula pravin.batt...@gmail.com wrote: Hi, I'm using cast to update values in a table by issuing following command. table.update().values(empno = cast(empno,Integer)).execute(). Where as empno is an string field,i'm trying to convert the data from empno column from string to integer and then issuing the below command to alter the data type of the column by issuing following command. alter_column(table.c.empno,type=Integer). It shows an error as OperationalError: (OperationalError) (1292, Truncated incorrect INTEGER value: '1d') 'UPDATE test.mytable SET `newColumn`=CAST(CAST(%s AS CHAR) AS SIGNED INTEGER)' ('1d',) Please do the needful -- You received this message because you are subscribed to the Google Groups sqlalchemy group. To post to this group, send email to sqlalchemy@googlegroups.com. To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en. -- You received this message because you are subscribed to the Google Groups sqlalchemy group. To post to this group, send email to sqlalchemy@googlegroups.com. To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en. -- You received this message because you are subscribed to the Google Groups sqlalchemy group. To post to this group, send email to sqlalchemy@googlegroups.com. To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.