Jump to content

Error on table creating


Recommended Posts

Posted

I'm trying to run this code on plugin install, but get an error:

5aae67307190d_ScreenShot2018-03-18at18_18_19.thumb.png.7f9ed253e7347d83209b5ccb4114872e.png

Code:

\IPS\Db::i()->createTable([
	'name' => 'sypexgeo_cache',
	'columns' => [
		\IPS\Db::i()->compileColumnDefinition([
			'name' => 'ip_address',
			'type' => 'VARCHAR',
			'length' => 46,
			'allow_null' => false,
			'comment' => 'The IP address'
		]),
		\IPS\Db::i()->compileColumnDefinition([
			'name' => 'data',
			'type' => 'TEXT',
			'allow_null' => true,
			'comment' => 'The JSON-encoded data returned by the service'
		]),
		\IPS\Db::i()->compileColumnDefinition([
			'name' => 'date',
			'type' => 'INT',
			'length' => 10,
			'allow_null' => false,
			'comment' => 'Unix timestamp of when the data was retrieved'
		])
	],
	'indexes' => [
		\IPS\Db::i()->compileIndexDefinition([
			'type' => 'primary',
			'name' => 'PRIMARY',
			'columns' => [
				'ip_address'
			],
			'length' => [
				null
			]
		]),
		\IPS\Db::i()->compileIndexDefinition([
			'type' => 'key',
			'name' => 'date',
			'columns' => [
				'date'
			],
			'length' => [
				null
			]
		])
	],
	'comment' => 'Sypexgeo cache table'
]);

It's a copy of IPS's core_geoip_cache table.

Posted
\IPS\Db::i()->createTable([
	'name' => 'sypexgeo_cache',
	'columns' => [
		'ip_address' =>
		[
			'name' => 'ip_address',
			'type' => 'VARCHAR',
			'length' => 46,
			'allow_null' => false,
			'comment' => 'The IP address'
		],
		'data' =>
		[
			'name' => 'data',
			'type' => 'TEXT',
			'allow_null' => true,
			'comment' => 'The JSON-encoded data returned by the service'
		],
		'date' =>
		[
			'name' => 'date',
			'type' => 'INT',
			'length' => 10,
			'allow_null' => false,
			'comment' => 'Unix timestamp of when the data was retrieved'
		]
	],
	'indexes' => [
		'PRIMARY' =>
		[
			'type' => 'primary',
			'name' => 'PRIMARY',
			'columns' => [
				'ip_address'
			],
			'length' => [
				null
			]
		],
		'date' =>	
		[
			'type' => 'key',
			'name' => 'date',
			'columns' => [
				'date'
			],
			'length' => [
				null
			]
		]
	],
	'comment' => 'Sypexgeo cache table'
]);

Don't forget try/catch

  • 2 weeks later...
Posted

BTW if you want to create a copy of any table structure use

\IPS\Db::i()->duplicateTableStructure('exists_table_name', 'new_table_name');

In your case 

\IPS\Db::i()->duplicateTableStructure('core_geoip_cache', 'sypexgeo_cache');

 

Archived

This topic is now archived and is closed to further replies.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...