[英]SQLAlchemy db interactions fail depending how I run the python script
This seems to be the most obscure issue I have dealt with so far. 到目前为止,这似乎是我处理过的最晦涩的问题。 I am struggling hard. 我努力奋斗。
I have an application that collects remote data from some hardware and saves it to a database. 我有一个从某些硬件收集远程数据并将其保存到数据库的应用程序。 I renamed one of the columns using alembic
. 我使用alembic
重命名了其中一列。 I have a development database and a testing database. 我有一个开发数据库和一个测试数据库。 Both are on the same server ( MySQL
via MariaDB
on CentOS 7
). 两者都在同一台服务器上( MySQL
通过CentOS 7
上的MariaDB
)。
I interact with the development server via the app running on my laptop and the testing db is interacted with via the app running on a clone of the production server. 我通过在笔记本电脑上运行的应用程序与开发服务器进行交互,并且通过在生产服务器的克隆上运行的应用程序与测试数据库进行交互。
All servers and databases are set up using Ansible
, so differences are limited to usernames and passwords. 所有服务器和数据库都是使用Ansible
设置的,因此区别仅限于用户名和密码。
Below is the ansible
snippet from the upgrade script 以下是升级脚本中的ansible
代码段
def upgrade():
op.alter_column('units', 'ip_address', new_column_name='ipv4', existing_type=sa.String(100))
If I run the app from my laptop (using my IDE), the data is saved ok. 如果我从笔记本电脑上运行该应用程序(使用我的IDE),则数据将保存正确。
If I run the script on the testing server manually (env)$ python app.py
, the data is saved ok. 如果我在测试服务器上手动(env)$ python app.py
运行脚本,则数据将保存正确。
But, here's the problem, If I run the script using supervisord
, I get an SQLAlchemy error. 但是,这就是问题所在,如果我使用supervisord
运行脚本,则会收到SQLAlchemy错误。 (excerpts below, full traceback ) (以下摘录, 完整的追溯 )
...
File "thefile.py", line 64, in run
self.data['my_wellsites'][w.name]['ipv4'] = wellsite.unit.ipv4
...
sqlalchemy.exc.InternalError: (InternalError) (1054, u"Unknown column 'units.ipv4' in 'field list'") 'SELECT units.id AS units_id, units.unittype_id AS units_unittype_id, units.serial AS units_serial, units.ipv4 AS units_ipv4, units.mac_address AS units_mac_address, units.engine_hours AS units_engine_hours, units.gen_odometer AS units_gen_odometer, units.gen_periodic AS units_gen_periodic, units.notes AS units_notes \nFROM units \nWHERE units.id = %s' (1,)
...
SQLAlchemy models.. SQLAlchemy模型
class Unit(Model):
__tablename__ = 'units'
__table_args__ = {'mysql_engine': 'InnoDB'}
id = Column(Integer, Sequence('unit_id_seq'), primary_key=True)
wellsites = relationship('Wellsite', order_by='Wellsite.id', backref='unit')
ipv4 = Column(String(16), unique=True)
...
class Wellsite(Model):
__tablename__ = 'wellsites'
__table_args__ = {'mysql_engine': 'InnoDB'}
id = Column(Integer, Sequence('wellsite_id_seq'), primary_key=True)
unit_id = Column(Integer, ForeignKey('units.id'), nullable=False)
...
etc/supervisord.conf.. 等/ supervisord.conf ..
...
[program:datacollect]
command = /home/webdev/mydevelopment/git/ers_data_app/env/bin/python /home/webdev/mydevelopment/git/ers_data_app/data_monitoring/collection_manager.py
stdout_logfile=/home/webdev/logs/datacollect.log
stderr_logfile=/home/webdev/logs/datacollecterr.log
autostart=true
autorestart=unexpected
startsecs=10
I tried running an additional alembic
upgrade with 我尝试使用运行额外的alembic
升级
op.create_unique_constraint('uq_ipv4', 'units', ['ipv4'])
No dice. 没有骰子。
The traceback
is identical (using diff program) except timestamps 除了时间戳, traceback
是相同的(使用diff程序)
Here are the two database descriptions of the units
table (identical) 这是units
表的两个数据库描述(相同)
MariaDB [ers_DEV]> show columns from units;
+--------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| unittype_id | int(11) | YES | MUL | NULL | |
| serial | varchar(10) | YES | UNI | NULL | |
| ipv4 | varchar(100) | YES | UNI | NULL | |
| mac_address | varchar(17) | YES | UNI | NULL | |
| engine_hours | int(11) | YES | | NULL | |
| gen_odometer | tinyint(1) | YES | | NULL | |
| gen_periodic | tinyint(1) | YES | | NULL | |
| notes | mediumtext | YES | | NULL | |
+--------------+--------------+------+-----+---------+----------------+
MariaDB [ers_TEST]> show columns from units;
+--------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| unittype_id | int(11) | YES | MUL | NULL | |
| serial | varchar(10) | YES | UNI | NULL | |
| ipv4 | varchar(100) | YES | UNI | NULL | |
| mac_address | varchar(17) | YES | UNI | NULL | |
| engine_hours | int(11) | YES | | NULL | |
| notes | mediumtext | YES | | NULL | |
| gen_odometer | tinyint(1) | YES | | NULL | |
| gen_periodic | tinyint(1) | YES | | NULL | |
+--------------+--------------+------+-----+---------+----------------+
The issue was at the top of the /etc.supervisord.conf
file. 该问题在/etc.supervisord.conf
文件的顶部。 In it, an environment variable is set that was pointing the script to the wrong database, overriding the environment variable that was set and examined from anywhere else. 在其中设置了一个环境变量,该环境变量将脚本指向错误的数据库,从而覆盖了从其他任何地方设置和检查的环境变量。 This environment variable is only set when the script is being run from supervisor
and was causing the trouble 仅在从supervisor
运行脚本并导致问题时才设置此环境变量
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.