Re: [sqlalchemy] Making Python3 list from set returned by SQL query

2019-03-11 Thread Rich Shepard

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

2019-03-11 Thread Simon King
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

2019-03-11 Thread Rich Shepard





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] Custom Handler for "No such polymorphic_identity"

2019-03-11 Thread Mike Bayer
On Mon, Mar 11, 2019 at 3:59 AM Tolstov Sergey  wrote:
>
> Can i override handler for this error?
> Example is - >
>   1) defined only Parent class
>   2) session.query(Parent)
>   -- > raised Exceptions
>
> Resolutions:
>   1) try_except for all_queries (too much entry points)
>   2) load all possible classes (low perfomance)
>
> How can i override /sqlalchemy/orm/loading.py
>   configure_subclass_mapper on runtime?

If you are loading a Parent class, and the row indicates that it is in
fact a Child class, then that mapper must be present.What would
this custom handler do exactly, run a whole module import inside the
loading process?   I'm not sure that's threadsafe in any case.if
your program runs long enough, all of those mappings would have been
loaded in any case I'm not sure I understand what the problem is.



>
> --
> 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 - 
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] Referencing the value of a table linked by a foreign Key

2019-03-11 Thread Simon King
On Sat, Mar 9, 2019 at 1:58 PM C  wrote:
>
> Hello every one,
>
> I am new on Python/SqlAlchemy and I try to develop an app but I can't find 
> how to display the name of people and not their foreignkey when I reference 
> them, could you help me ?
>
> Here are my classes :
>
>
>
> from application.main import db
>
>
> class Lettre(db.Model):
> __tablename__ = "lettre"
> lettre_id = db.Column(db.Integer, unique=True, nullable=False, 
> primary_key=True)
> titre = db.Column(db.Text, nullable=False)
> contenu = db.Column(db.Text, nullable=False)
> date_label = db.Column(db.Text, nullable=False)
> date_norm = db.Column(db.Text, nullable=False)
> lettre_expediteur = db.Column(db.Text, 
> db.ForeignKey('correspondant.nom'), autoincrement='ignore_fk')
> lettre_destinataire=db.Column(db.Text, 
> db.ForeignKey('correspondant.nom'), autoincrement='ignore_fk')
> depuis_lieu=db.Column(db.Text, db.ForeignKey('lieu.lieu_id'))
> vers_lieu=db.Column(db.Text, db.ForeignKey('lieu.lieu_id'))
>
>
>
> # Nous avons ici créé une première classe (table) pour notre base de données.
> class Correspondant(db.Model):
> __tablename__ = "correspondant"
> id_correspondant = db.Column(db.Integer, unique=True, nullable=False, 
> primary_key=True)
> nom = db.Column(db.Text, nullable=False)
> prenom = db.Column(db.Text, nullable=False)
>
>
>
> class Lieu(db.Model):
> __tablename__  = "lieu"
> lieu_id=db.Column(db.Integer, unique=True, nullable=False, 
> primary_key=True)
> label=db.Column(db.Integer, unique=True, nullable=False)
>
>
>
>
>
>
> And here is what I wrote in my HTML pages :
>
>
> 
> {{correspondance.titre}}
> 
>Expéditeur 
> {{correspondance.lettre_expediteur}}
> Date {{correspondance.date_label}}
> Destinataire 
> {{correspondance.lettre_destinataire}}
> Contenu {{correspondance.contenu}}
> 
>
>
> But I always get the foreign key and not the name (nom) of the correspondant.
> Could you help me please ?

You need to create a "relationship" between your 2 classes. This is
separate from the foreign key (although they often use your foreign
key definitions as the basis for the relationship).

https://docs.sqlalchemy.org/en/latest/orm/tutorial.html#building-a-relationship

Hope that helps,

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

2019-03-11 Thread Simon King
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] Re: Custom Handler for "No such polymorphic_identity"

2019-03-11 Thread Tolstov Sergey
Another example is
class cls1 (Base)
  ...

class cls1_child(cls1)
  ...

class cls2(Base)
  cls1_rel = sqlalchemy.rel(cls1, ...)
  ...

a = session.query(cls2)
a.cls1_rel - > throw exception if return cls1_child( and it is not loaded 
before)

-- 
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] Re: Custom Handler for "No such polymorphic_identity"

2019-03-11 Thread Tolstov Sergey
Another example is
cls1

cls1_child(cls1)

cls2
  cls1_rel = sqlalchemy.rel(cls1, ...)


a = session.queyr(cls2)
a.cls1_rel - > throw exception if return cls1_child( and it is not loaded 
before)

-- 
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] Custom Handler for "No such polymorphic_identity"

2019-03-11 Thread Tolstov Sergey
Can i override handler for this error?
Example is - >
  1) defined only Parent class
  2) session.query(Parent)
  -- > raised Exceptions

Resolutions:
  1) try_except for all_queries (too much entry points)
  2) load all possible classes (low perfomance)

How can i override /sqlalchemy/orm/loading.py
  configure_subclass_mapper on runtime?

-- 
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.