Flask sqlalchemy many-to-many insert dataFlask-SQLAlchemy Many to Many InsertFlask-appbuilder many-to-may relationship sqlalchemy.exc.NoReferencedTableError ErrorSQLAlchemy insert rows to bridge tableFlask sqlalchemy many-to-many update dataSqlAlchemy KeyError: 'post' while using .append() or .extend() in many-many relationWhy can't Python parse this JSON data?SQLAlchemy ORDER BY DESCENDING?SQLAlchemy: What's the difference between flush() and commit()?jsonify a SQLAlchemy result set in FlaskHow to get data received in Flask requestflask-sqlalchemy or sqlalchemyHow to execute raw SQL in SQLAlchemy-flask appSQLAlchemy generating query in loop with containsHow to delete a record by id in Flask-SQLAlchemyFlask-msearch python , adding msearch on many to many table

Can I interfere when another PC is about to be attacked?

How old can references or sources in a thesis be?

Why is an old chain unsafe?

How is it possible to have an ability score that is less than 3?

How did the USSR manage to innovate in an environment characterized by government censorship and high bureaucracy?

declaring a variable twice in IIFE

Is there really no realistic way for a skeleton monster to move around without magic?

Set-theoretical foundations of Mathematics with only bounded quantifiers

Pronouncing Dictionary.com's W.O.D "vade mecum" in English

Why has Russell's definition of numbers using equivalence classes been finally abandoned? ( If it has actually been abandoned).

N.B. ligature in Latex

Why Is Death Allowed In the Matrix?

Why is the design of haulage companies so “special”?

A Journey Through Space and Time

Draw simple lines in Inkscape

TGV timetables / schedules?

I’m planning on buying a laser printer but concerned about the life cycle of toner in the machine

Copenhagen passport control - US citizen

How do we improve the relationship with a client software team that performs poorly and is becoming less collaborative?

What is the command to reset a PC without deleting any files

How do I create uniquely male characters?

How to make payment on the internet without leaving a money trail?

What do you call something that goes against the spirit of the law, but is legal when interpreting the law to the letter?

How does one intimidate enemies without having the capacity for violence?



Flask sqlalchemy many-to-many insert data


Flask-SQLAlchemy Many to Many InsertFlask-appbuilder many-to-may relationship sqlalchemy.exc.NoReferencedTableError ErrorSQLAlchemy insert rows to bridge tableFlask sqlalchemy many-to-many update dataSqlAlchemy KeyError: 'post' while using .append() or .extend() in many-many relationWhy can't Python parse this JSON data?SQLAlchemy ORDER BY DESCENDING?SQLAlchemy: What's the difference between flush() and commit()?jsonify a SQLAlchemy result set in FlaskHow to get data received in Flask requestflask-sqlalchemy or sqlalchemyHow to execute raw SQL in SQLAlchemy-flask appSQLAlchemy generating query in loop with containsHow to delete a record by id in Flask-SQLAlchemyFlask-msearch python , adding msearch on many to many table






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








55















I am trying to make a many to many relation here in Flask-SQLAlchemy, but it seems that I don't know how to fill the "many to many identifier database". Could you please help me understand what I am doing wrong and how it is supposed to look?



class User(db.Model):
__tablename__ = 'users'
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)


and then my identifier database:



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('users.user_id'))
)


so far it looks like this when I try to insert the data into the database.



# User
user1 = User(
user_fistName='John',
user_lastName='Doe',
user_email='john@doe.es')

user2 = User(
user_fistName='Jack',
user_lastName='Doe',
user_email='jack@doe.es')

user3 = User(
user_fistName='Jane',
user_lastName='Doe',
user_email='jane@doe.es')

db.session.add_all([user1, user2, user3])
db.session.commit()

# Class
cl1 = Class(class_name='0A')
cl2 = Class(class_name='0B')
cl3 = Class(class_name='0C')
cl4 = Class(class_name='Math')
cl5 = Class(class_name='Spanish')
db.session.add_all([cl1, cl2, cl3, cl4, cl5])
db.session.commit()


Now my problem is, how do I add to the many to many database, since I really can't create a 'student_identifier' object? If I could it could perhaps have looked like this:



# Student Identifier
sti1 = StiClass(class_id=cl1.class_id, class_name=user1.user_id)
sti2 = StiClass(class_id=cl3.class_id, class_name=user1.user_id)
sti3 = StiClass(class_id=cl4.class_id, class_name=user1.user_id)
sti4 = StiClass(class_id=cl2.class_id, class_name=user2.user_id)
db.session.add_all([sti1, sti2, sti3, sti4])
db.session.commit()


How I am supposed to insert into a many to many table with ORM?










share|improve this question






























    55















    I am trying to make a many to many relation here in Flask-SQLAlchemy, but it seems that I don't know how to fill the "many to many identifier database". Could you please help me understand what I am doing wrong and how it is supposed to look?



    class User(db.Model):
    __tablename__ = 'users'
    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)


    and then my identifier database:



    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('users.user_id'))
    )


    so far it looks like this when I try to insert the data into the database.



    # User
    user1 = User(
    user_fistName='John',
    user_lastName='Doe',
    user_email='john@doe.es')

    user2 = User(
    user_fistName='Jack',
    user_lastName='Doe',
    user_email='jack@doe.es')

    user3 = User(
    user_fistName='Jane',
    user_lastName='Doe',
    user_email='jane@doe.es')

    db.session.add_all([user1, user2, user3])
    db.session.commit()

    # Class
    cl1 = Class(class_name='0A')
    cl2 = Class(class_name='0B')
    cl3 = Class(class_name='0C')
    cl4 = Class(class_name='Math')
    cl5 = Class(class_name='Spanish')
    db.session.add_all([cl1, cl2, cl3, cl4, cl5])
    db.session.commit()


    Now my problem is, how do I add to the many to many database, since I really can't create a 'student_identifier' object? If I could it could perhaps have looked like this:



    # Student Identifier
    sti1 = StiClass(class_id=cl1.class_id, class_name=user1.user_id)
    sti2 = StiClass(class_id=cl3.class_id, class_name=user1.user_id)
    sti3 = StiClass(class_id=cl4.class_id, class_name=user1.user_id)
    sti4 = StiClass(class_id=cl2.class_id, class_name=user2.user_id)
    db.session.add_all([sti1, sti2, sti3, sti4])
    db.session.commit()


    How I am supposed to insert into a many to many table with ORM?










    share|improve this question


























      55












      55








      55


      23






      I am trying to make a many to many relation here in Flask-SQLAlchemy, but it seems that I don't know how to fill the "many to many identifier database". Could you please help me understand what I am doing wrong and how it is supposed to look?



      class User(db.Model):
      __tablename__ = 'users'
      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)


      and then my identifier database:



      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('users.user_id'))
      )


      so far it looks like this when I try to insert the data into the database.



      # User
      user1 = User(
      user_fistName='John',
      user_lastName='Doe',
      user_email='john@doe.es')

      user2 = User(
      user_fistName='Jack',
      user_lastName='Doe',
      user_email='jack@doe.es')

      user3 = User(
      user_fistName='Jane',
      user_lastName='Doe',
      user_email='jane@doe.es')

      db.session.add_all([user1, user2, user3])
      db.session.commit()

      # Class
      cl1 = Class(class_name='0A')
      cl2 = Class(class_name='0B')
      cl3 = Class(class_name='0C')
      cl4 = Class(class_name='Math')
      cl5 = Class(class_name='Spanish')
      db.session.add_all([cl1, cl2, cl3, cl4, cl5])
      db.session.commit()


      Now my problem is, how do I add to the many to many database, since I really can't create a 'student_identifier' object? If I could it could perhaps have looked like this:



      # Student Identifier
      sti1 = StiClass(class_id=cl1.class_id, class_name=user1.user_id)
      sti2 = StiClass(class_id=cl3.class_id, class_name=user1.user_id)
      sti3 = StiClass(class_id=cl4.class_id, class_name=user1.user_id)
      sti4 = StiClass(class_id=cl2.class_id, class_name=user2.user_id)
      db.session.add_all([sti1, sti2, sti3, sti4])
      db.session.commit()


      How I am supposed to insert into a many to many table with ORM?










      share|improve this question
















      I am trying to make a many to many relation here in Flask-SQLAlchemy, but it seems that I don't know how to fill the "many to many identifier database". Could you please help me understand what I am doing wrong and how it is supposed to look?



      class User(db.Model):
      __tablename__ = 'users'
      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)


      and then my identifier database:



      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('users.user_id'))
      )


      so far it looks like this when I try to insert the data into the database.



      # User
      user1 = User(
      user_fistName='John',
      user_lastName='Doe',
      user_email='john@doe.es')

      user2 = User(
      user_fistName='Jack',
      user_lastName='Doe',
      user_email='jack@doe.es')

      user3 = User(
      user_fistName='Jane',
      user_lastName='Doe',
      user_email='jane@doe.es')

      db.session.add_all([user1, user2, user3])
      db.session.commit()

      # Class
      cl1 = Class(class_name='0A')
      cl2 = Class(class_name='0B')
      cl3 = Class(class_name='0C')
      cl4 = Class(class_name='Math')
      cl5 = Class(class_name='Spanish')
      db.session.add_all([cl1, cl2, cl3, cl4, cl5])
      db.session.commit()


      Now my problem is, how do I add to the many to many database, since I really can't create a 'student_identifier' object? If I could it could perhaps have looked like this:



      # Student Identifier
      sti1 = StiClass(class_id=cl1.class_id, class_name=user1.user_id)
      sti2 = StiClass(class_id=cl3.class_id, class_name=user1.user_id)
      sti3 = StiClass(class_id=cl4.class_id, class_name=user1.user_id)
      sti4 = StiClass(class_id=cl2.class_id, class_name=user2.user_id)
      db.session.add_all([sti1, sti2, sti3, sti4])
      db.session.commit()


      How I am supposed to insert into a many to many table with ORM?







      python flask sqlalchemy flask-sqlalchemy






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 27 '17 at 8:56









      duan

      4,58532549




      4,58532549










      asked Sep 4 '14 at 14:28









      SigilsSigils

      85831330




      85831330






















          2 Answers
          2






          active

          oldest

          votes


















          95














          You don't need to add anything directly to your association table, SQLAlchemy will do that. This is more or less from SQLAlchemy documentations:



          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()


          Therefore your sample would be like this:



          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()





          share|improve this answer
































            4














            First off, student_identifier is defined as a SQLAlchemy reflection table not a database.



            Normally if you have all the relationship setup properly between models and reflection table objects, you will only need to deal with related models (by appending model objects into the relationship InstrumentList) in order to insert data into reflection tables, for instance, the answer @mehdi-sadeghi provided above.



            However, there is indeed a way to insert directly into reflection tables if you don't want to setup the relationship. For example:



            statement = student_identifier.insert().values(class_id=cl1.id, user_id=sti1.id)
            db.session.execute(statement)
            db.session.commit()


            After that, you should be able to see that a many-to-many relationship row is inserted into the student_identifier reflection table. Don't forget to commit after you execute each SQL statement as it's done in a transaction.



            Hope that helps you with an alternative approach.






            share|improve this answer























            • how can I import this reflection table or association table?

              – jibin mathew
              Dec 20 '18 at 19:34











            • @jibinmathew just as you would with classes and other symbols.

              – Devy
              Jan 2 at 15:54











            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f25668092%2fflask-sqlalchemy-many-to-many-insert-data%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            95














            You don't need to add anything directly to your association table, SQLAlchemy will do that. This is more or less from SQLAlchemy documentations:



            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()


            Therefore your sample would be like this:



            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()





            share|improve this answer





























              95














              You don't need to add anything directly to your association table, SQLAlchemy will do that. This is more or less from SQLAlchemy documentations:



              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()


              Therefore your sample would be like this:



              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()





              share|improve this answer



























                95












                95








                95







                You don't need to add anything directly to your association table, SQLAlchemy will do that. This is more or less from SQLAlchemy documentations:



                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()


                Therefore your sample would be like this:



                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()





                share|improve this answer















                You don't need to add anything directly to your association table, SQLAlchemy will do that. This is more or less from SQLAlchemy documentations:



                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()


                Therefore your sample would be like this:



                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()






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Mar 22 at 0:35









                cowgill

                12828




                12828










                answered Sep 4 '14 at 15:21









                Mehdi SadeghiMehdi Sadeghi

                2,8451828




                2,8451828























                    4














                    First off, student_identifier is defined as a SQLAlchemy reflection table not a database.



                    Normally if you have all the relationship setup properly between models and reflection table objects, you will only need to deal with related models (by appending model objects into the relationship InstrumentList) in order to insert data into reflection tables, for instance, the answer @mehdi-sadeghi provided above.



                    However, there is indeed a way to insert directly into reflection tables if you don't want to setup the relationship. For example:



                    statement = student_identifier.insert().values(class_id=cl1.id, user_id=sti1.id)
                    db.session.execute(statement)
                    db.session.commit()


                    After that, you should be able to see that a many-to-many relationship row is inserted into the student_identifier reflection table. Don't forget to commit after you execute each SQL statement as it's done in a transaction.



                    Hope that helps you with an alternative approach.






                    share|improve this answer























                    • how can I import this reflection table or association table?

                      – jibin mathew
                      Dec 20 '18 at 19:34











                    • @jibinmathew just as you would with classes and other symbols.

                      – Devy
                      Jan 2 at 15:54















                    4














                    First off, student_identifier is defined as a SQLAlchemy reflection table not a database.



                    Normally if you have all the relationship setup properly between models and reflection table objects, you will only need to deal with related models (by appending model objects into the relationship InstrumentList) in order to insert data into reflection tables, for instance, the answer @mehdi-sadeghi provided above.



                    However, there is indeed a way to insert directly into reflection tables if you don't want to setup the relationship. For example:



                    statement = student_identifier.insert().values(class_id=cl1.id, user_id=sti1.id)
                    db.session.execute(statement)
                    db.session.commit()


                    After that, you should be able to see that a many-to-many relationship row is inserted into the student_identifier reflection table. Don't forget to commit after you execute each SQL statement as it's done in a transaction.



                    Hope that helps you with an alternative approach.






                    share|improve this answer























                    • how can I import this reflection table or association table?

                      – jibin mathew
                      Dec 20 '18 at 19:34











                    • @jibinmathew just as you would with classes and other symbols.

                      – Devy
                      Jan 2 at 15:54













                    4












                    4








                    4







                    First off, student_identifier is defined as a SQLAlchemy reflection table not a database.



                    Normally if you have all the relationship setup properly between models and reflection table objects, you will only need to deal with related models (by appending model objects into the relationship InstrumentList) in order to insert data into reflection tables, for instance, the answer @mehdi-sadeghi provided above.



                    However, there is indeed a way to insert directly into reflection tables if you don't want to setup the relationship. For example:



                    statement = student_identifier.insert().values(class_id=cl1.id, user_id=sti1.id)
                    db.session.execute(statement)
                    db.session.commit()


                    After that, you should be able to see that a many-to-many relationship row is inserted into the student_identifier reflection table. Don't forget to commit after you execute each SQL statement as it's done in a transaction.



                    Hope that helps you with an alternative approach.






                    share|improve this answer













                    First off, student_identifier is defined as a SQLAlchemy reflection table not a database.



                    Normally if you have all the relationship setup properly between models and reflection table objects, you will only need to deal with related models (by appending model objects into the relationship InstrumentList) in order to insert data into reflection tables, for instance, the answer @mehdi-sadeghi provided above.



                    However, there is indeed a way to insert directly into reflection tables if you don't want to setup the relationship. For example:



                    statement = student_identifier.insert().values(class_id=cl1.id, user_id=sti1.id)
                    db.session.execute(statement)
                    db.session.commit()


                    After that, you should be able to see that a many-to-many relationship row is inserted into the student_identifier reflection table. Don't forget to commit after you execute each SQL statement as it's done in a transaction.



                    Hope that helps you with an alternative approach.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Sep 22 '17 at 20:54









                    DevyDevy

                    5,20323846




                    5,20323846












                    • how can I import this reflection table or association table?

                      – jibin mathew
                      Dec 20 '18 at 19:34











                    • @jibinmathew just as you would with classes and other symbols.

                      – Devy
                      Jan 2 at 15:54

















                    • how can I import this reflection table or association table?

                      – jibin mathew
                      Dec 20 '18 at 19:34











                    • @jibinmathew just as you would with classes and other symbols.

                      – Devy
                      Jan 2 at 15:54
















                    how can I import this reflection table or association table?

                    – jibin mathew
                    Dec 20 '18 at 19:34





                    how can I import this reflection table or association table?

                    – jibin mathew
                    Dec 20 '18 at 19:34













                    @jibinmathew just as you would with classes and other symbols.

                    – Devy
                    Jan 2 at 15:54





                    @jibinmathew just as you would with classes and other symbols.

                    – Devy
                    Jan 2 at 15:54

















                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f25668092%2fflask-sqlalchemy-many-to-many-insert-data%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

                    용인 삼성생명 블루밍스 목차 통계 역대 감독 선수단 응원단 경기장 같이 보기 외부 링크 둘러보기 메뉴samsungblueminx.comeh선수 명단용인 삼성생명 블루밍스용인 삼성생명 블루밍스ehsamsungblueminx.comeheheheh

                    155 수학 과학 기타 둘러보기 메뉴eh추가해eh문서를 완성해