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;
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
add a comment |
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
add a comment |
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
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
python flask sqlalchemy flask-sqlalchemy
edited Nov 27 '17 at 8:56
duan
4,58532549
4,58532549
asked Sep 4 '14 at 14:28
SigilsSigils
85831330
85831330
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
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()
add a comment |
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.
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
add a comment |
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
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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()
add a comment |
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()
add a comment |
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()
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()
edited Mar 22 at 0:35
cowgill
12828
12828
answered Sep 4 '14 at 15:21
Mehdi SadeghiMehdi Sadeghi
2,8451828
2,8451828
add a comment |
add a comment |
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.
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
add a comment |
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.
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
add a comment |
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.
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.
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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