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.


[sqlalchemy] Referencing the value of a table linked by a foreign Key

2019-03-09 Thread C
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 ?

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