I am unable to make reference between two tables work using
flask-sqlalchemy. Please find attached the file.
I get an error "expected string or buffer". Please, could someone tell me
what I am doing wrong? And the best way to go about it. Thanks
--
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 [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.
from flask import Flask
from flask.ext.sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://root:@localhost/test'
app.config['SQLALCHEMY_ECHO'] = True
db = SQLAlchemy(app)
class User(db.Model):
__tablename__ = 'User'
ID = db.Column(db.Integer, primary_key=True)
FirstName = db.Column(db.String(64))
LastName = db.Column(db.String(64))
Email = db.Column(db.String(64), unique=True)
PwdHash = db.Column(db.String(100))
Payments = db.relationship('Payment', backref='payer', lazy='dynamic', foreign_keys='Payment.uidPayer')
Received = db.relationship('Payment', backref='receiver', lazy='dynamic', foreign_keys='Payment.uidReceiver')
def __init__(self, FirstName, LastName, Email, PwdHash):
self.FirstName = FirstName
self.LastName = LastName
self.Email = Email
self.PwdHash = PwdHash
class Payment(db.Model):
__tablename__ = "Payment"
id = db.Column(db.Integer, primary_key=True)
uidPayer = db.Column(db.Integer, db.ForeignKey("User.ID"))
uidReceiver = db.Column(db.Integer, db.ForeignKey("User.ID"))
amount = db.Column(db.Float)
def __init__(self, uidPayer, uidReceiver, amount):
self.uidPayer = uidPayer
self.uidReceiver = uidReceiver
self.amount = amount
def db_init():
db.drop_all()
db.create_all()
if __name__ == '__main__':
try:
db_init()
u1 = User('Coffee', 'Shop', '[email protected]', '12677563I7^54')
u2 = User('Coder', 'Shop', '[email protected]', '1254677563I7^')
p1 = Payment(u1, u2, 25)
db.session.add_all((u1, p1))
db.session.commit()
except Exception as e:
print('e = {}'.format(e))
db.session.rollback()
db.session.flush()