Note that there are some explanatory texts on larger screens.

plurals
  1. POcreate new SQLalchemy object fails when referenceing uid
    primarykey
    data
    text
    <p>New python and SQLAlchemy user here (yeah i know :)) anyway, I am trying to create some tables using a user, sensor, and reading table where both user and sensor have a one to many relationship to a reading.</p> <p>My classes are like this:</p> <pre><code>class User(db.Model): __tablename__ = 'user' uid = db.Column(db.Integer, primary_key=True, index=True) firstName = db.Column(db.String(100)) lastName = db.Column(db.String(100)) emailAddress = db.Column(db.String(120), unique=True, index=True) pwHash = db.Column(db.String(256)) userLevel = db.Column(db.Integer()) userAccountType = db.Column(db.Integer()) isUserActive = db.Column(db.Boolean()) isUserLockedOut = db.Column(db.Boolean()) userLastLogin = db.Column(db.DateTime()) lastInvalidLogin = db.Column(db.DateTime()) userCreatedAt = db.Column(db.DateTime()) userConfirmedAt = db.Column(db.DateTime()) userUpdatedAt = db.Column(db.DateTime(), onupdate=datetime.datetime.now()) userAddress = db.relationship('Address', backref='user', lazy='dynamic') userContactMethod = db.relationship('UserContactMethod', backref='user', lazy='dynamic') userSensor = db.relationship('Sensor', backref='user', lazy='dynamic') userReading = db.relationship('Reading', backref='user', lazy='dynamic') deliveryEvents = db.relationship('logSMTPDeliveryEvents', backref='user', lazy='dynamic') class Reading(db.Model): __tablename__ = 'reading' rid = db.Column(db.Integer, primary_key=True) uid = db.Column(db.Integer, db.ForeignKey('user.uid')) sid = db.Column(db.Integer, db.ForeignKey('sensor.sid')) readingTimestamp = db.Column(db.DateTime()) readingLightValue = db.Column(db.Integer) readingLightStatus = db.Column(db.String(6)) readingTemp1 = db.Column(db.Float) readingTemp2 = db.Column(db.Float) readingHumidity = db.Column(db.Float) def __init__(self, uid, sid, readingTimestamp, readingLightValue, readingLightStatus, readingTemp1, readingTemp2, readingHumidity): self.uid = uid self.sid = sid self.readingTimestamp = readingTimestamp self.readingLightValue = readingLightValue self.readingLightStatus = readingLightStatus self.readingTemp1 = readingTemp1 self.readingTemp2 = readingTemp2 self.readingHumidity = readingHumidity </code></pre> <p>So, if I try and create on object like this:</p> <pre><code> newReading = Reading(uid=10, sid=123,readingTimestamp = readingTimestamp, readingLightValue=readingLightValue, readingLightStatus=readingLightStatus, readingTemp1=readingTemp1, readingTemp2=readingTemp2, readingHumidity=readingHumidity) db.session.add(newReading) db.session.commit() </code></pre> <p>Where I hardcode the uid everything works fine. Hoewever if I try and use my query before that like so:</p> <pre><code> userID = db.session.query(User.uid).filter(User.emailAddress == userEmailAddress).first() </code></pre> <p>and then I use the userID variable in my create object like this:</p> <pre><code> newReading = Reading(uid=userID, sid=123,readingTimestamp = readingTimestamp, readingLightValue=readingLightValue, readingLightStatus=readingLightStatus, readingTemp1=readingTemp1, readingTemp2=readingTemp2, readingHumidity=readingHumidity) </code></pre> <p>I get the following error:</p> <pre><code>IntegrityError: (IntegrityError) (1452, 'Cannot add or update a child row: a foreign key constraint fails (`gnomecentral`.`reading`, CONSTRAINT `reading_ibfk_1` FOREIGN KEY (`uid`) REFERENCES `user` (`uid`))') 'INSERT INTO reading (uid, sid, `readingTimestamp`, `readingLightValue`, `readingLightStatus`, `readingTemp1`, `readingTemp2`, `readingHumidity`) VALUES (%s, %s, %s, %s, %s, %s, %s, %s)' ((10L,), 123, datetime.datetime(2013, 9, 14, 14, 48, 7, 684071), 43, '43', 43.0, 43.0, 43.0) </code></pre> <p>Thanks in advance for the help! Craig</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload