【发布时间】:2021-12-22 19:26:05
【问题描述】:
我使用 sql 查询动态创建表,所以我创建了StockSample,我想克隆它的结构,但它不会将默认 id 作为主键并自动递增它
这是我的代码,
cursor = connection.cursor()
query= "select * into stock_"+str(stock_id)+" from stocksample where 1=2;"
cursor.execute(query)
SQL 查询创建的内容:
库存样本:
型号:
class StockSample(models.Model):
user_id = models.ForeignKey(AppUser,on_delete=models.CASCADE)
product_name = models.CharField(max_length=100)
product_id = models.ForeignKey(Product,on_delete=models.CASCADE)
barcode = models.CharField(max_length=100, null=True, blank=True)
mrp = models.IntegerField()
selling_price = models.IntegerField()
expiry_date = models.DateField(null=True, blank=True)
试过这个但它仍然没有将 id 作为主键:
query = "Select *, id = ROW_NUMBER() OVER(ORDER BY(SELECT NULL)) Into stock_"+str(stock_id)+" From stocksample Where 1 = 2"query2 = "alter table stock_"+str(stock_id)+" add primary key id" GIVES ERROR
【问题讨论】:
-
你想在那里实现什么?看起来您正在滚动自己的分区实现。为什么不简单地使用内置分区?
标签: sql django postgresql django-models