如何在 mysql 中的两个不同模式中使用两个具有相同名称的表与 django
How to use two tables with same name in two different schema in mysql with django
我正在使用两个 mysql 模式 X 和 Y,它们都包含多个 table,但有一个 table 在两个模式中具有相同的名称。
两种模式如下:
+--------------+--+--------------+
| X | | Y |
+--------------+--+--------------+
| name | | album_info |
+--------------+--+--------------+
| invite | | photo_info |
+--------------+--+--------------+
| photo | | photo |
+--------------+--+--------------+
| user_details | | temp |
+--------------+--+--------------+
现在,我想查询两个 table,但是当我在 models.py 文件中写入具有相同名称的 table 结构时,它会抛出 error/exception。
我在 routers.py 文件中声明了两个 table 如下:
modelDatabaseMap = {
.
'photo': 'default',
.
.
.
'photo': 'y',
}
(X 是我的默认模式)。
声明我 models.py 如下:
class Photo(models.Model):
id = models.AutoField(db_column='ID', primary_key=True)
has_tagged_with = models.IntegerField()
has_reposted_with = models.IntegerField()
.
.
class Meta:
managed = False
db_table = 'photo'
class Photo(models.Model):
id = models.AutoField(db_column='ID', primary_key=True)
account_id = models.IntegerField()
p_id = models.IntegerField()
is_profile = models.IntegerField()
.
.
class Meta:
managed = False
db_table = 'photo'
现在,歧义首先是名称,models.py中的声明,其次是查询。
我对如何通过 orm 分别查询 table 感到困惑。 关于此的任何 help/lead 都会有所帮助。提前致谢。
鉴于您的 DATABASES
配置,您可以尝试:
更改您的模型名称或为每个模式创建一个具有不同 models
模块的新应用:
class YPhoto(models.Model):
...
class XPhoto(models.Model):
...
创建一个 router.py
模块:
class MyRouter(object):
def db_for_read(self, model, **hints):
if model.__name__ == 'YPhoto':
return 'Y'
return None
def db_for_write(self, model, **hints):
if model.__name__ == 'YPhoto':
return 'Y'
return None
def allow_relation(self, obj1, obj2, **hints):
# Maybe you want to prevent relations between different schemas, it's up to you
return True
def allow_syncdb(self, db, model):
return True
添加路由器到settings.py
:
DATABASE_ROUTERS = ['myapp.models.MyRouter',]
检查docs关于数据库路由。
我正在使用两个 mysql 模式 X 和 Y,它们都包含多个 table,但有一个 table 在两个模式中具有相同的名称。
两种模式如下:
+--------------+--+--------------+
| X | | Y |
+--------------+--+--------------+
| name | | album_info |
+--------------+--+--------------+
| invite | | photo_info |
+--------------+--+--------------+
| photo | | photo |
+--------------+--+--------------+
| user_details | | temp |
+--------------+--+--------------+
现在,我想查询两个 table,但是当我在 models.py 文件中写入具有相同名称的 table 结构时,它会抛出 error/exception。 我在 routers.py 文件中声明了两个 table 如下:
modelDatabaseMap = {
.
'photo': 'default',
.
.
.
'photo': 'y',
}
(X 是我的默认模式)。 声明我 models.py 如下:
class Photo(models.Model):
id = models.AutoField(db_column='ID', primary_key=True)
has_tagged_with = models.IntegerField()
has_reposted_with = models.IntegerField()
.
.
class Meta:
managed = False
db_table = 'photo'
class Photo(models.Model):
id = models.AutoField(db_column='ID', primary_key=True)
account_id = models.IntegerField()
p_id = models.IntegerField()
is_profile = models.IntegerField()
.
.
class Meta:
managed = False
db_table = 'photo'
现在,歧义首先是名称,models.py中的声明,其次是查询。 我对如何通过 orm 分别查询 table 感到困惑。 关于此的任何 help/lead 都会有所帮助。提前致谢。
鉴于您的 DATABASES
配置,您可以尝试:
更改您的模型名称或为每个模式创建一个具有不同
models
模块的新应用:class YPhoto(models.Model): ... class XPhoto(models.Model): ...
创建一个
router.py
模块:class MyRouter(object): def db_for_read(self, model, **hints): if model.__name__ == 'YPhoto': return 'Y' return None def db_for_write(self, model, **hints): if model.__name__ == 'YPhoto': return 'Y' return None def allow_relation(self, obj1, obj2, **hints): # Maybe you want to prevent relations between different schemas, it's up to you return True def allow_syncdb(self, db, model): return True
添加路由器到
settings.py
:DATABASE_ROUTERS = ['myapp.models.MyRouter',]
检查docs关于数据库路由。