我需要在数据库级别获取包含主键的列名。
Model.primary_key
返回models_id
列名,该列名不是在数据库级别上为主键。我仍然无法更改迁移或修改表。
我目前可以使用ActiveRecord::Base.connection.primary_key('table_name')
通过MySqlAdapter来获取它
,但这不适用于具有复合主键的表。如果表包含复合主键,则它返回nil
。
我可以通过编程方式实现此目的吗?
答案 0 :(得分:2)
ActiveRecord不支持开箱即用的复合主键。
您需要安装composite_primary_keys gem,然后如果主键是复合键,ActiveRecord::Base.connection.primary_key('table_name')
将返回一个列名数组。
答案 1 :(得分:0)
执行查询:
<ScreenManagement>:
MenuScreen:
name: 'menu'
CalculatorScreen:
name: 'calculator'
<MenuScreen>:
BoxLayout:
Button:
text: 'Goto Calculator'
on_press: root.manager.current = 'calculator'
Button:
text: 'Quit'
<CalculatorScreen>:
Calculator:
<CustButton@Button>:
font_size: 35
background_color: 0,0,0,0
canvas.before:
Color:
rgba: (.4, .4, .4, 1) if self.state=='normal' else (0,.7,.7,1)
RoundedRectangle:
pos: self.pos
size: self.size
radius: [20, ]
<RawLayout@BoxLayout>:
spacing: 8
padding: 8
size_hint: [1, .2]
<Calculator>:
id: calculator
rows: 10
display: entry
spacing: 1
BoxLayout:
size_hint: [1, .1]
Label:
text: 'Basic Calculator'
Label:
text: 'Made by Xrew'
BoxLayout:
padding: 10
TextInput:
id: entry
spacing: 1
padding: 5
font_size: 32
multiline: True
focus: False
# background_color: 0, 0, 0, 1
# foreground_color: [1, 0, 1, 1]
RawLayout:
CustButton:
text: '<'
on_press: entry.text += self.text
CustButton:
text: '>'
on_press: entry.text += self.text
CustButton:
text: '≈'
on_press: entry.text += '=='
RawLayout:
orientation: 'horizontal'
CustButton:
text: '('
on_press: entry.text += '('
CustButton:
text: ')'
on_press: entry.text += ')'
CustButton:
text: '√'
on_press: entry.text += '**(.5)'
CustButton:
text: '¹/x'
on_press: entry.text += '1/'
RawLayout:
orientation: 'horizontal'
CustButton:
text: 'Del'
on_press: entry.text = entry.text[:-1]
CustButton:
text: 'x²'
on_press: entry.text += '**2'
CustButton:
text: 'xⁿ'
on_press: entry.text += '**'
CustButton:
text: 'π'
on_press: entry.text += '3.14'
RawLayout:
orientation: 'horizontal'
cols: 4
CustButton:
text: 'Clr'
font_color: [255,0,0,1]
# background_normal: ' '
# background_color: 1, .3, .4, .85
on_press: entry.text = ""
CustButton:
text: '+'
on_press: entry.text += self.text
font_size: 32
CustButton:
text: '÷'
on_press: entry.text += '/'
CustButton:
text: '×'
on_press: entry.text += '*'
RawLayout:
rows: 1
orientation: 'horizontal'
CustButton:
text: '7'
on_press: entry.text += self.text
CustButton:
text: '8'
on_press: entry.text += self.text
CustButton:
text: '9'
on_press: entry.text += self.text
CustButton:
text: '-'
on_press: entry.text += self.text
RawLayout:
orientation: 'horizontal'
rows: 1
CustButton:
text: '4'
on_press: entry.text += self.text
CustButton:
text: '5'
on_press: entry.text += self.text
CustButton:
text: '6'
on_press: entry.text += self.text
CustButton:
text: '+'
on_press: entry.text += self.text
RawLayout:
orientation: 'horizontal'
cols: 3
CustButton:
text: '1'
size_hint: [.5, 1]
on_press: entry.text += self.text
CustButton:
text: '2'
size_hint: [.5, 1]
on_press: entry.text += self.text
CustButton:
text: '3'
size_hint: [.5, 1]
on_press: entry.text += self.text
CustButton:
text: ' '
size_hint: [.5, 1]
background_normal: ' '
background_color: 0, 0, 0, 0
RawLayout:
orientation: 'horizontal'
size_hint: [1, .2]
CustButton:
text: '0'
on_press: entry.text += self.text
size_hint: [.34, 1]
CustButton:
text: '.'
on_press: entry.text += self.text
size_hint: [.17, 1]
font_size: 32
CustButton:
text: '='
on_press: calculator.calculate(entry.text)
size_hint: [.17, 2.4]
# background_normal: ' '
# background_color: 0, .5, 95, 1
然后查看返回的所有行,其中名为 KEY 的列的值为'PRI'。然后,字段列将为您提供表中构成表主键一部分的列名。
或者:
SHOW COLUMNS FROM table_name;