default value dont work in sqlalchemy python - python

Good day!
This is my table
class My_table(Base):
__tablename__ = 'table'
__table_args__ = {'mysql_engine': 'InnoDB',
"mysql_collate": "utf8mb4_general_ci",
"mysql_charset": "utf8mb4"}
id = Column(INTEGER(unsigned=True), primary_key=True, autoincrement=True)
sport = Column(VARCHAR(30), nullable=False)
active = Column(TINYINT(), nullable=False, default=1, server_default=text('1'))
this is result of model
CREATE TABLE `table` (
`id` int unsigned NOT NULL AUTO_INCREMENT,
`sport` varchar(30) COLLATE utf8mb4_general_ci NOT NULL,
`active` tinyint NOT NULL DEFAULT '1',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;
sport = {'sport': Football, 'active': 0 }
sport1 = {'sport': Football}
If i add dict sport then all good , but if i try add sport1 then i got pymysql.err.IntegrityError: (1048, "Column 'active' cannot be null")
If
active = Column(TINYINT(), default='1', server_default=text('1'))
active = Column(TINYINT(), default=1, server_default=text('1'))
active = Column(TINYINT(), default='1', server_default='1')
Then in base column active have NULL. How does it work and what am I doing wrong ?

Related

Integrity Error when creating multiple records with Flask SqlAlchemy

I have a function that creates some new DB entries in Flask app with SQL Alchemy
def add_volunteer_client_record(volunteer_id, **kwargs):
try:
volunteer_client = VolunteerClient(volunteer_id=volunteer_id, **kwargs)
volunteer_report_action_items = VolunteerReportActionItems(volunteer_client_id = volunteer_client.id)
db_session.add(volunteer_client)
db_session.add(volunteer_report_action_items)
db_session.commit()
return volunteer_client
except IntegrityError as e:
db_session.rollback()
message = "Integrity error occurred"
raise BadRequestError(messages={'volunteer_client': [message]})
volunteer_client gets created fine but when volunteer_report_action_items is added to the session I received an IntegrityError and I can not quite understand why.
My Models
class VolunteerClient(Base):
__tablename__ = 'volunteer_client'
id = Column(Integer, primary_key=True, autoincrement=True, index=True)
volunteer_id = Column(Integer, ForeignKey('provider_user.user_id', onupdate='CASCADE', ondelete='RESTRICT'), unique=True)
client_id = Column(Integer, ForeignKey('user.id', onupdate='CASCADE', ondelete='RESTRICT'), unique=True)
class VolunteerReportActionItems(Base):
__tablename__ = 'volunteer_report_action_items'
id = Column(Integer, primary_key=True, autoincrement=True, index=True)
volunteer_client_id = Column(Integer, ForeignKey('volunteer_client.id', onupdate='CASCADE', ondelete='RESTRICT'))
SQL
CREATE TABLE IF NOT EXISTS public.volunteer_client
(
id serial PRIMARY KEY,
volunteer_id integer NOT NULL,
client_id integer NOT NULL,
created_by text,
created_at timestamp NOT NULL DEFAULT now(),
updated_by text,
updated_at timestamp NOT NULL DEFAULT now(),
UNIQUE(volunteer_id, client_id),
CONSTRAINT fk_volunteer_client_volunteer FOREIGN KEY (volunteer_id)
REFERENCES public.user (id) MATCH SIMPLE
ON UPDATE CASCADE
ON DELETE RESTRICT,
CONSTRAINT fk_volunteer_client_client FOREIGN KEY (client_id)
REFERENCES public.user (id) MATCH SIMPLE
ON UPDATE CASCADE
ON DELETE RESTRICT
);
ALTER TABLE public.volunteer_client OWNER to navigate;
CREATE TABLE IF NOT EXISTS public.volunteer_report_action_items
(
id serial PRIMARY KEY,
volunteer_client_id integer NOT NULL,
created_by text,
created_at timestamp NOT NULL DEFAULT now(),
updated_by text,
updated_at timestamp NOT NULL DEFAULT now(),
CONSTRAINT fk_volunteer_report_action_items_volunteer_client FOREIGN KEY (volunteer_client_id)
REFERENCES public.volunteer_client (id) MATCH SIMPLE
ON UPDATE CASCADE
ON DELETE RESTRICT
);
ALTER TABLE public.volunteer_report_action_items OWNER to navigate;
Any help or advice here would be great. Thanks!
You need to flush the VolunteerClient to be able to access the VolunteerClient.id
def add_volunteer_client_record(volunteer_id, **kwargs):
try:
volunteer_client = VolunteerClient(volunteer_id=volunteer_id, **kwargs)
db_session.add(volunteer_client)
db_session.flush()
volunteer_report_action_items = VolunteerReportActionItems(volunteer_client_id = volunteer_client.id)
db_session.add(volunteer_report_action_items)
db_session.commit()
...

Foreign Keys not lining up in raw SQL and FLASK SQL Alchemy

I have somewhat of a general question, also curious about best practices. I have a model in SQL which looks as so:
CREATE TABLE IF NOT EXISTS public.volunteer_report
(
id serial PRIMARY KEY,
volunteer_id integer NOT NULL,
client_id integer NOT NULL,
user_need_id integer NOT NULL,
report character varying(255) NOT NULL,
report_category character varying(255) NOT NULL,
created_by text,
created_at timestamp NOT NULL DEFAULT now(),
updated_by text,
updated_at timestamp NOT NULL DEFAULT now(),
CONSTRAINT fk_volunteer_report_volunteer FOREIGN KEY (volunteer_id)
REFERENCES public.user (id) MATCH SIMPLE
ON UPDATE CASCADE
ON DELETE RESTRICT,
CONSTRAINT fk_volunteer_report_client FOREIGN KEY (client_id)
REFERENCES public.user (id) MATCH SIMPLE
ON UPDATE CASCADE
ON DELETE RESTRICT,
CONSTRAINT fk_volunteer_report_client_need FOREIGN KEY (user_need_id)
REFERENCES public.user_need (id) MATCH SIMPLE
ON UPDATE CASCADE
ON DELETE RESTRICT
);
Both FK's make a reference to the user PK.
MY FLASK APP
class VolunteerReport(Base):
__tablename__ = 'volunteer_report'
id = Column(Integer, primary_key=True, autoincrement=True, index=True)
volunteer_id = Column(Integer, ForeignKey('volunteer_client.volunteer_id', onupdate='CASCADE', ondelete='RESTRICT'))
client_id = Column(Integer, ForeignKey('volunteer_client.client_id', onupdate='CASCADE', ondelete='RESTRICT'))
user_need_id = Column(Integer, ForeignKey('user_need.id', onupdate='CASCADE', ondelete='RESTRICT'))
report = Column(String(255), nullable=False)
report_category = Column(String(255), nullable=False)
The FK references are different here. referencing volunteer_client table instead of user table, but the int FK value that gets stored is the same.
Is this expected to work and perform fine, or should the table references be exactly identical?

Django Foreign key constraint error on submitting a row

I just created a Django Project and added 2 directly in mysql:
1) Financial Holdings (tbl_holdings)
2) Service Providers (tbl_holdings_service_providers)
Both of their Data Definitions in My Sql are:
tbl_holdings
CREATE TABLE `tbl_holdings` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`item_code` varchar(5) NOT NULL,
`product_name` varchar(45) NOT NULL,
`service_provider` varchar(100) NOT NULL,
`account_details` varchar(100) NOT NULL,
`purchase_cost` int(15) NOT NULL,
`current_value` int(15) NOT NULL,
`purchase_date` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`created_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
UNIQUE KEY `item_code_UNIQUE` (`item_code`),
KEY `fk_service_provider_name_idx` (`service_provider`),
CONSTRAINT `fk_service_provider` FOREIGN KEY (`service_provider`) REFERENCES `tbl_holdings_service_providers` (`provider_name`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1
tbl_holdings_service_providers
CREATE TABLE `tbl_holdings_service_providers` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`provider_name` varchar(100) NOT NULL,
`updated_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`created_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
KEY `INDEX` (`provider_name`)
) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=latin1
I created the related model in Django models.py
class TblHoldings(models.Model):
item_code = models.CharField(unique=True, max_length=5)
product_name = models.CharField(max_length=45)
service_provider = models.ForeignKey('TblHoldingsServiceProviders', models.DO_NOTHING, related_name='service_provider',db_column='service_provider')
account_details = models.CharField(max_length=100)
purchase_cost = models.IntegerField()
current_value = models.IntegerField()
purchase_date = models.DateTimeField(blank=True, null=True)
updated_at = models.DateTimeField(blank=True, null=True)
created_at = models.DateTimeField(blank=True, null=True)
class Meta:
verbose_name = 'Holding'
verbose_name_plural = 'Holdings'
managed = False
db_table = 'tbl_holdings'
class TblHoldingsServiceProviders(models.Model):
provider_name = models.CharField(max_length=100)
updated_at = models.DateTimeField(blank=True, null=True)
created_at = models.DateTimeField(blank=True, null=True)
class Meta:
verbose_name = "Lookup For Service Provider"
verbose_name_plural = 'Lookup For Service Providers'
managed = False
db_table = 'tbl_holdings_service_providers'
and the code in admin.py is
#admin.register(TblHoldings)
class HoldingsAdmin(admin.ModelAdmin):
list_display = ('item_code', 'product_name', 'service_provider', 'current_value', 'created_at')
ordering = ('purchase_date',)
search_fields = ('product_name',)
#admin.register(TblHoldingsServiceProviders)
class HoldingsServiceProviderAdmin(admin.ModelAdmin):
list_display = ('id', 'provider_name', 'created_at')
ordering = ('created_at',)
search_fields = ('provider_name',)
On trying to add a holding I get the below error: pertaining to the foreign key constraint
IntegrityError at /admin/app_fin/tblholdings/add/ (1452, 'Cannot add
or update a child row: a foreign key constraint fails
(_finance.tbl_holdings, CONSTRAINT fk_service_provider
FOREIGN KEY (service_provider) REFERENCES
tbl_holdings_service_providers (provider_name) ON DELETE NO ACTION
ON UPDATE )') Request Method: POST Request
URL: admin/app_fin/tblholdings/add/ Django
Version: 2.2.7 Exception Type: IntegrityError Exception Value: (1452,
'Cannot add or update a child row: a foreign key constraint fails
(_finance.tbl_holdings, CONSTRAINT fk_service_provider
FOREIGN KEY (service_provider) REFERENCES
tbl_holdings_service_providers (provider_name) ON DELETE NO ACTION
ON UPDATE )') Exception
Location: /root//venv/lib/python3.6/site-packages/MySQLdb/connections.py
in query, line 231 Python
Executable: /root//venv/bin/python Python Version: 3.6.8
Python Path: ['/root//project_',
'/root//venv/lib/python36.zip',
'/root//venv/libremoved/python3.6',
'/root//venv/lib/python3.6/lib-dynload',
'/usr/lib/python3.6',
'/root/_fin/venv/lib/python3.6/site-packages'] Server
time: Tue, 12 Nov 2019 09:51:28 +0000
Can someone point me to what I am doing wrong.
Looks like the foreign key you are using is not a primary key. You can try this out
CREATE TABLE `tbl_holdings` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`item_code` varchar(5) NOT NULL,
`product_name` varchar(45) NOT NULL,
`service_provider` varchar(100) NOT NULL,
`account_details` varchar(100) NOT NULL,
`purchase_cost` int(15) NOT NULL,
`current_value` int(15) NOT NULL,
`purchase_date` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`created_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (`id`),
UNIQUE KEY `item_code_UNIQUE` (`item_code`),
KEY `fk_service_provider_name_idx` (`service_provider`),
CONSTRAINT `fk_service_provider` FOREIGN KEY (`service_provider`) REFERENCES `tbl_holdings_service_providers` (`provider_id`) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1

How to use sqlalchemy to query in many-to-many relation?

To get a member list in current organization, i used following statement,
from ..members import members
from flask.ext.login import current_user
from app.common.database import db_session
#members.route("/", methods=["GET"])
#login_required
def index():
datas = db_session.query(Group_has_Person).filter(Group_has_Person.group.organization==current_user.organization).all()
but a exception threw out at runtime: AttributeError: Neither 'InstrumentedAttribute' object nor 'Comparator' object associated with Group_has_Person.group has an attribute 'organization'
how to get the member list in a right way? the code below is about model definition:
class Organization(Base):
__tablename__ = 'Organization'
id = Column(Integer, primary_key = True)
title = Column(String(45), nullable = False)
logo = Column(String(256), nullable = False)
contact_name = Column(String(45), nullable = False)
contact_phone = Column(String(45), nullable = False)
created_time = Column(DateTime, nullable = False, default = datetime.now())
User_id = Column(Integer, ForeignKey('User.id'))
user = relationship("User", back_populates="organization")
groups = relationship("Group", back_populates="organization")
class Group(Base):
__tablename__ = 'Group'
id = Column(Integer, primary_key = True)
title = Column(String(45), nullable = False)
teacher = Column(String(45), nullable = True)
contact = Column(String(45), nullable = True)
created_time = Column(DateTime, nullable = False, default = datetime.now())
status = Column(Integer, nullable = False, default = 1)
Organization_id = Column(Integer, ForeignKey('Organization.id'))
organization = relationship("Organization", back_populates="groups")
members = relationship("Group_has_Person", back_populates="group")
class Person(Base):
__tablename__ = 'Person'
id = Column(Integer, primary_key = True)
nickname = Column(String(45), nullable = False)
avatar = Column(String(256), nullable = False)
gender = Column(Integer, nullable = False)
birthday = Column(DateTime, nullable = False)
User_id = Column(Integer, ForeignKey('User.id'))
user = relationship("User", back_populates="person")
groups = relationship("Group_has_Person", back_populates="person")
class Group_has_Person(Base):
__tablename__ = 'Group_has_Person'
Group_id = Column(Integer, ForeignKey('Group.id'), primary_key = True)
Person_id = Column(Integer, ForeignKey('Person.id'), primary_key = True)
created_time = Column(DateTime, nullable = False, default = datetime.now())
status = Column(Integer, nullable = False, default = 0)
group = relationship("Group", back_populates="members")
person = relationship("Person", back_populates="groups")
table scripts:
CREATE TABLE 'Group' (
'id' int(11) NOT NULL AUTO_INCREMENT,
'title' varchar(45) NOT NULL DEFAULT '' COMMENT '班级名称',
'teacher' varchar(45) DEFAULT NULL COMMENT '辅导老师',
'contact' varchar(45) DEFAULT NULL COMMENT '联系方式',
'created_time' datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
'status' int(11) NOT NULL COMMENT '状态:-1-删除 1-正常',
'Organization_id' int(11) NOT NULL,
PRIMARY KEY ('id'),
KEY 'fk_Classes_Organization1_idx' ('Organization_id'),
CONSTRAINT 'fk_Classes_Organization1' FOREIGN KEY ('Organization_id') REFERENCES 'Organization' ('id') ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8 COMMENT='班级';
CREATE TABLE 'Person' (
'id' int(11) NOT NULL AUTO_INCREMENT,
'User_id' int(11) NOT NULL,
'nickname' varchar(45) NOT NULL COMMENT '',
'avatar' varchar(256) NOT NULL COMMENT '',
'gender' int(11) NOT NULL COMMENT '',
'birthday' datetime DEFAULT NULL COMMENT '',
PRIMARY KEY ('id'),
KEY 'fk_Person_User1_idx' ('User_id'),
CONSTRAINT 'fk_Person_User1' FOREIGN KEY ('User_id') REFERENCES 'User' ('id') ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8 COMMENT='个人';
CREATE TABLE 'Group_has_Person' (
'Group_id' int(11) NOT NULL,
'Person_id' int(11) NOT NULL,
'created_time' datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '加入时间',
'status' int(11) NOT NULL DEFAULT '0' COMMENT '状态 0-申请 1-成功',
PRIMARY KEY ('Group_id','Person_id'),
KEY 'fk_Group_has_Person_Person1_idx' ('Person_id'),
KEY 'fk_Group_has_Person_Group1_idx' ('Group_id'),
CONSTRAINT 'fk_Group_has_Person_Group1' FOREIGN KEY ('Group_id') REFERENCES 'Group' ('id') ON DELETE NO ACTION ON UPDATE NO ACTION,
CONSTRAINT 'fk_Group_has_Person_Person1' FOREIGN KEY ('Person_id') REFERENCES 'Person' ('id') ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
I've resolved my question by the following statement:
datas = db_session.query(Group_has_Person).join(Group).filter(Group.organization==current_user.organization).all()

django model foreign key created index

I am creating models in django. This is my config in the models file. I want to create a table governmentoffical that references counties and citizen. However, when I ran manage.py sqlal, it changed my column name in government official tables.
class counties(models.Model):
name = models.CharField(max_length=50, primary_key=True, unique=True)
population = models.IntegerField()
governer = models.CharField(max_length=75)
class citizen(models.Model):
ssn = models.CharField(max_length=40, primary_key=True)
citizen_name = models.CharField(max_length=50, unique=True)
age = models.IntegerField()
income = models.IntegerField()
username = models.CharField(max_length=30)
password = models.CharField(max_length=30)
class governmentOfficial(models.Model):
countyName = models.ForeignKey(counties, primary_key=True)
offical_name = models.ForeignKey(citizen, to_field='citizen_name')
position = models.CharField(max_length=50)
year_elected = models.IntegerField(max_length=4)
party = models.CharField(max_length=25)
county_username=models.CharField(max_length=20)
county_password=models.CharField(max_length=20)
Here is my sql output.
CREATE TABLE `troywebsite_governmentofficial` (
`countyName_id` varchar(50) NOT NULL PRIMARY KEY,
`offical_name_id` varchar(50) NOT NULL,
`position` varchar(50) NOT NULL,
`year_elected` integer NOT NULL,
`party` varchar(25) NOT NULL,
`county_username` varchar(20) NOT NULL,
`county_password` varchar(20) NOT NULL
)
;
ALTER TABLE `troywebsite_governmentofficial` ADD CONSTRAINT `countyName_id_refs_name_2c9a2c94` FOREIGN KEY (`countyName_id`) REFERENCES `troywebsite_counties` (`name`);
ALTER TABLE `troywebsite_governmentofficial` ADD CONSTRAINT `offical_name_id_refs_citizen_name_6f795c2a` FOREIGN KEY (`offical_name_id`) REFERENCES `troywebsite_citizen` (`citizen_name`);
CREATE INDEX `troywebsite_governmentofficial_effdd6a5` ON `troywebsite_governmentofficial` (`offical_name_id`);
CREATE INDEX `troywebsite_solider_c5b4e228` ON `troywebsite_solider` (`solider_name_id`);
Is there a way to take out the CREATE INDEX in governmentofficial, and leave to to be countyName and Official_name as the column?
You can use the db_column parameter to call the column whatever you like.
https://docs.djangoproject.com/en/dev/ref/models/fields/#db-column
countyName = models.ForeignKey(counties, primary_key=True, db_column='whatever')

Categories

Resources