Re: [sqlalchemy] Making Python3 list from set returned by SQL query
On Mon, 11 Mar 2019, Simon King wrote: Using your Industries class, a function to return that list of names could look like this: def get_industry_names(session): q = session.query(Industries) return [i.ind_name for i in q.all()] Does that do what you want? Simon, It probably will, but I don't yet have sufficient code to test it. I'm now writing the views for all classes (which is what prompted my question) and will test this function as soon as practical. Thanks very much, Rich -- 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] Making Python3 list from set returned by SQL query
On Mon, Mar 11, 2019 at 2:18 PM Rich Shepard wrote: > > > > > I'm not sure exactly what you mean here. You can write a function that > > iterates over the rows and selects the attributes that you want. > > Simon, > > Let me try to be more clear. > > There's a database table named 'organizations', and an SA model class > associated with it. One of the columns in the organization class, industry, > is defined as: > > industry = Column(String, default='Other', >ForeignKey('industries.ind_name', onupdate="CASCADE", > ondelete="RESTRICT")) > > The industry classe is defined prior to the organization class: > > class Industries(Base): > __tablename__ = 'industries' > > ind_name = Column(String, primary_key=True) > > When I query the database table using psql, > select * from industries order by ind_name; > psql displays this: > > ind_name > - > Attorney > Business > Consultant > Energy > Farming > Forest products > Government > Livestock > Manufacturing > Maritime > Mining > Other > (12 rows) > > My assumption is that SA will return the same results. I want to present > these names in the tkinter form for the organizations class within a > ttk.Combobox by dropping the first two, and last, rows and making a list of > the ind_name strings. There is another column in this class (and one in > another class) where acceptable values for those variables are also in > lookup tables. > > What is the most efficient way to do this? I thought that a function which > ignores the first two rows then addes all but the last to a list would do > the job so confirmation is helpful. > > If I'm still not explaining well enough I'll try again. :-) > > Regards, > > Rich Using your Industries class, a function to return that list of names could look like this: def get_industry_names(session): q = session.query(Industries) return [i.ind_name for i in q.all()] Does that do what you want? 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.
Re: [sqlalchemy] Making Python3 list from set returned by SQL query
I'm not sure exactly what you mean here. You can write a function that iterates over the rows and selects the attributes that you want. Simon, Let me try to be more clear. There's a database table named 'organizations', and an SA model class associated with it. One of the columns in the organization class, industry, is defined as: industry = Column(String, default='Other', ForeignKey('industries.ind_name', onupdate="CASCADE", ondelete="RESTRICT")) The industry classe is defined prior to the organization class: class Industries(Base): __tablename__ = 'industries' ind_name = Column(String, primary_key=True) When I query the database table using psql, select * from industries order by ind_name; psql displays this: ind_name - Attorney Business Consultant Energy Farming Forest products Government Livestock Manufacturing Maritime Mining Other (12 rows) My assumption is that SA will return the same results. I want to present these names in the tkinter form for the organizations class within a ttk.Combobox by dropping the first two, and last, rows and making a list of the ind_name strings. There is another column in this class (and one in another class) where acceptable values for those variables are also in lookup tables. What is the most efficient way to do this? I thought that a function which ignores the first two rows then addes all but the last to a list would do the job so confirmation is helpful. If I'm still not explaining well enough I'll try again. :-) Regards, Rich -- 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] Making Python3 list from set returned by SQL query
On Fri, Mar 8, 2019 at 10:54 PM Rich Shepard wrote: > > Two classes in the model have columns with values constrained to specific > text strings; one table has two such columns, another table has one. > > Because new strings might be added to the lists of allowed values for that > column each has the acceptable values as rows in a table rather than in a > column constraint. > > SQL queries processed by SA to select rows in each table return sets. How do > I transpose each set to a python list can will be used by tkinter to display > them in a ttk.Combobox? I'm not sure exactly what you mean here. You can write a function that iterates over the rows and selects the attributes that you want. For something more automated, you could use an association proxy, as in the "Simplifying Scalar Collections" example at https://docs.sqlalchemy.org/en/latest/orm/extensions/associationproxy.html#simplifying-scalar-collections > > Because these lists need to be present before the classes for the associated > tables are processed how should the queries and lists be represented in the > view module? In a separate class? Normally, your class definitions correspond to the "shape" of the database (ie. the tables), rather than the contents of those tables, so I don't really know what you mean by "the lists need to be present before the classes for the associated tables are processed". Can you give an example? Thanks, 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] Making Python3 list from set returned by SQL query
Two classes in the model have columns with values constrained to specific text strings; one table has two such columns, another table has one. Because new strings might be added to the lists of allowed values for that column each has the acceptable values as rows in a table rather than in a column constraint. SQL queries processed by SA to select rows in each table return sets. How do I transpose each set to a python list can will be used by tkinter to display them in a ttk.Combobox? Because these lists need to be present before the classes for the associated tables are processed how should the queries and lists be represented in the view module? In a separate class? If the answers are in the SA docs please point me to them. And, if my questions are not sufficiently clear let me know and I'll try to be more explicit. TIA, Rich -- 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.