Search
 
SCRIPT & CODE EXAMPLE
 

SQL

many to many flask-sqlalchemy

association_table = db.Table('association', db.Model.metadata,
    db.Column('left_id', db.Integer, db.ForeignKey('left.id')),
    db.Column('right_id', db.Integer, db.ForeignKey('right.id'))
)

class Parent(db.Model):
    __tablename__ = 'left'
    id = db.Column(db.Integer, primary_key=True)
    children = db.relationship("Child",
                    secondary=association_table)

class Child(db.Model):
    __tablename__ = 'right'
    id = db.Column(db.Integer, primary_key=True)


p = Parent()
c = Child()
p.children.append(c)
db.session.add(p)
db.session.commit()
Comment

many to many flask-sqlalchemy

student_identifier = db.Table('student_identifier',
    db.Column('class_id', db.Integer, db.ForeignKey('classes.class_id')),
    db.Column('user_id', db.Integer, db.ForeignKey('students.user_id'))
)

class Student(db.Model):
    __tablename__ = 'students'
    user_id = db.Column(db.Integer, primary_key=True)
    user_fistName = db.Column(db.String(64))
    user_lastName = db.Column(db.String(64))
    user_email = db.Column(db.String(128), unique=True)


class Class(db.Model):
    __tablename__ = 'classes'
    class_id = db.Column(db.Integer, primary_key=True)
    class_name = db.Column(db.String(128), unique=True)
    students = db.relationship("Student",
                               secondary=student_identifier)

s = Student()
c = Class()
c.students.append(s)
db.session.add(c)
db.session.commit()
Comment

PREVIOUS NEXT
Code Example
Sql :: remove decimal in sql server 
Sql :: in in sql 
Sql :: query inner join 
Sql :: sqlplus change user 
Sql :: insert query in oracle 
Sql :: mssql now diff 90 day 
Sql :: java sql connection close 
Sql :: create fulltext index mysql 
Sql :: create directory in sql server 
Sql :: offset in postgresql example 
Sql :: postgresql array to rows 
Sql :: make selected text uppercase mssql 
Sql :: .env pgsql 
Sql :: keep getting an error when I try to connect to mysql workbench 
Sql :: postgresql alter column data type from integer to integer array 
Sql :: what is subquery in sql 
Sql :: change column in mysql 
Sql :: select only columns that are not empty oracle sql 
Sql :: mysql multiple left joins on same table 
Sql :: what does leave do in mysql 
Sql :: sql is null and is not null 
Sql :: sql transfer table in other 
Sql :: mysql privileges 
Sql :: sql ignore 
Sql :: drop constraint in ms sql 
Sql :: sql remove duplicates based on column 
Sql :: asp.net core sql server stored procedure 
Sql :: insert sql 
Sql :: group_concat sql server 
Sql :: mysql login 
ADD CONTENT
Topic
Content
Source link
Name
1+2 =